in reply to File Open Issue

I know I could use a link to the file like "../info.txt" but it doesn't open the file, unless the path is absolute to the file.

Where did you get that idea? Of course you can use . and .. directories in a path to a file for opening.

Since you don't state where the files' directory is relative to the cgi-bin directory, it is difficult to give very precise advice. However, in general, use ../ to go up one level in the directory hierarchy and the exact directory name to go down.

Say you have a directory structure like this:

|--apache2--|---cgi-bin
            |
            |---htdocs
            |
            |---images

or whatever. To access a file from the htdocs directory, starting at the cgi-bin directory, you would specify: '../htdocs/info.txt'. Substituted back into your script:

use warnings; use strict; $0 =~ '(.*[\\\/])\w+\.\w+$'; my $curr_dir = $1 || "./"; # Note, you would be much better off with # use Cwd; # and # my $curr_dir = cwd(); $info_file = $curr_dir . '../htdocs/info.txt'; open my $fh, '<', $info_file or die "Could not open file, $!"; while (my $line = <$fh>){ #process line }

Replies are listed 'Best First'.
Re^2: File Open Issue
by Anonymous Monk on Jun 01, 2005 at 12:38 UTC
    Your code doesn't work, here is what I am trying:
    use Cwd 'chdir'; chdir "../htdocs/"; open (FILE, "<$ENV{'PWD'}info.txt") || print "There is no file here in + $ENV{'PWD'}info.txt"; while ( my $line = <FILE> ) { chomp($line); #code.... }

      Sigh. My fault for posting untested code.
      You are still missing the point about using Cwd. You don't need the chdir and you shouldn't rely on $ENV{PWD}. Here is a (tested) version showing what I mean.

      use warnings; use strict; use Cwd; my $curr_dir = cwd(); my $info_file = $curr_dir . '/../htdocs/info.txt'; open my $fh, '<', $info_file or die "Could not open file, $!"; while (my $line = <$fh>){ # process text }