Relative path wont work when opening a file for writing

Discussion in 'Perl Misc' started by skieros, Apr 24, 2007.

  1. skieros

    skieros Guest

    open(FILE, ">>/some_folder/some_sub_folder/digest.passwd") or die $!;
    print FILE "$user:$realm:" . Digest::MD5::md5_hex("$user:$realm:
    $pass") . "\n";
    close(FILE);

    Hello i have the above code that i have changed from absolute hdd path
    to a relative to a web server path, but after the change the file
    cannot be opened.

    Problem is that cause of the script runnign both in localhost and
    remote server
    i cant specify the same absolute path so i need some relativity here.

    I could use a varibale after determiantion of *where* the scrpt is
    running, that is which host, but i want to accomplish it with a
    relative path. Is this possible?!
     
    skieros, Apr 24, 2007
    #1
    1. Advertising

  2. skieros wrote:
    > open(FILE, ">>/some_folder/some_sub_folder/digest.passwd") or die $!;
    > print FILE "$user:$realm:" . Digest::MD5::md5_hex("$user:$realm:
    > $pass") . "\n";
    > close(FILE);
    >
    > Hello i have the above code that i have changed from absolute hdd path
    > to a relative to a web server path, but after the change the file
    > cannot be opened.
    >
    > Problem is that cause of the script runnign both in localhost and
    > remote server
    > i cant specify the same absolute path so i need some relativity here.
    >
    > I could use a varibale after determiantion of *where* the scrpt is
    > running, that is which host, but i want to accomplish it with a
    > relative path. Is this possible?!
    >


    It'll depend on your webserver, but something like

    use strict;
    use warnings;

    my %paths = (
    relative => q(some_folder/asdf.txt),
    absolute => q(/other_path/some_folder/asdf.txt),
    );
    my $filepath;

    if($ENV{SERVER_NAME} eq 'localhost'){
    $filepath = $paths{relative};
    }else{
    $filepath = $paths{absolute};
    }

    open my $file,">>",$filepath or die $!;
     
    Mark Clements, Apr 24, 2007
    #2
    1. Advertising

  3. skieros

    J. Gleixner Guest

    Mark Clements wrote:
    > skieros wrote:
    >> open(FILE, ">>/some_folder/some_sub_folder/digest.passwd") or die $!;
    >> print FILE "$user:$realm:" . Digest::MD5::md5_hex("$user:$realm:
    >> $pass") . "\n";
    >> close(FILE);
    >>
    >> Hello i have the above code that i have changed from absolute hdd path
    >> to a relative to a web server path, but after the change the file
    >> cannot be opened.


    Why not? What's the error?

    >>
    >> Problem is that cause of the script runnign both in localhost and
    >> remote server
    >> i cant specify the same absolute path so i need some relativity here.
    >>
    >> I could use a varibale after determiantion of *where* the scrpt is
    >> running, that is which host, but i want to accomplish it with a
    >> relative path. Is this possible?!
    >>

    >
    > It'll depend on your webserver, but something like
    >
    > use strict;
    > use warnings;
    >
    > my %paths = (
    > relative => q(some_folder/asdf.txt),
    > absolute => q(/other_path/some_folder/asdf.txt),
    > );
    > my $filepath;
    >
    > if($ENV{SERVER_NAME} eq 'localhost'){
    > $filepath = $paths{relative};
    > }else{
    > $filepath = $paths{absolute};
    > }
    >
    > open my $file,">>",$filepath or die $!;


    That's fine as long as the path exists and is writable by the user
    running the process.
     
    J. Gleixner, Apr 24, 2007
    #3
  4. skieros

    skieros Guest

    On Apr 24, 11:11 pm, "J. Gleixner" <glex_no-s...@qwest-spam-
    no.invalid> wrote:
    > Mark Clements wrote:
    > > skieros wrote:


    > >> Hello i have the above code that i have changed from absolute hdd path
    > >> to a relative to a web server path, but after the change the file
    > >> cannot be opened.

    >
    > Why not? What's the error?


    Perl cannot find the file to open.
     
    skieros, Apr 27, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    19,567
    Sudsy
    Jul 8, 2004
  2. Thomas Guettler
    Replies:
    3
    Views:
    761
    Andrei
    Oct 27, 2003
  3. spike grobstein
    Replies:
    5
    Views:
    347
    Fredrik Lundh
    Dec 7, 2005
  4. Oren
    Replies:
    1
    Views:
    551
    Michael D. Ober
    Apr 29, 2007
  5. Nigel Wilkinson
    Replies:
    2
    Views:
    389
    Nigel Wilkinson
    Jul 25, 2005
Loading...

Share This Page