JaredHess has asked for the wisdom of the Perl Monks concerning the following question:

Hi again. Perl newbie struggling to learn more Wisdom... Urrg. I'm probably missing something very basic, that I can't see. . . Here's what's happening:

I'm using a form that allows a user to browse to a file on their local system and upload it to our webserver. When I tested it on my local server it works fine.

However, when I test it on our actual web server, the Open function that reads the file (just prior to uploading it) seems to be looking for the file on the server, instead of on the local system, because it always dies saying it can't find the file.

I need it to read the specified user's local file as specified in the form's <input type="file"> tag. It dies in this line:

open (IN,$fullpath) || dienice("Couldn't open file ** $fullpath ** f +or reading: $!");
What am I missing? Here's my upload code snippet:
$uploadpath = "newsletters/"; # Fullpath comes from file specified in the form $file = $fullpath; $file=~s/^.*(\\|\/)//; # STRIP PATHWAY OFF! # binmode $fullfile; $file = lc($file); #print "<p>Testing...file is: $file"; $buffer = ''; #print "<p>Testing...uploadpath is: $uploadpath"; #print "<p>Testing...Filehandle is: $uploadpath" . "$file"; # Open the file for reading open (IN,$fullpath) || dienice("Couldn't open file ** $fullpath ** f +or reading: $!"); # Open the directory for writing to open (SAVEFILE, ">$uploadpath"."$file") || dienice("Can't open fileh +andle: $uploadpath" . "$file $!\n"); binmode SAVEFILE; $i=0; while ($bytes = read(IN, $buffer, 1024)) { $bytesread += $bytes; print SAVEFILE $buffer; } close(SAVEFILE) || dienice ("Can't close filehandle: $uploadpath/$fi +le $!\n"); close (IN);

Replies are listed 'Best First'.
Re: Open(IN,$fullpath) can't find local file
by mpeters (Chaplain) on Jan 11, 2005 at 20:03 UTC
    When you are using a web form and a <input type='file' *> tag you never write the code that reads the file from the user's system. That would be a big security no-no if it were possible. That is taken care of by the web browser. I am assuming that you are trying to upload that file to the web server.

    You should look at CGI. It has some good example's of uploading files.
      Yes, you are correct. I'm trying to upload the file. I don't wnat to write code to make it find the local file, but I'm mystified as to why it can't find it. Unless I'm making a mistake somewhere (a verly likely reason), it should be grabbing the local file from my system instead of trying to find it on the web server...
Re: Open(IN,$fullpath) can't find local file
by gellyfish (Monsignor) on Jan 12, 2005 at 09:41 UTC

    It wo0rks when you run it on your local server (ie when browser and web server are on the same machine) because both the browser and the CGI program have access to the same filesystems and the filename parameter is therefore meaningful to both. However, obviously, when browser and CGI program are running on different machines the CGI program generally won't have access to the same filesystems, this why the browser uploads the contents of the file in the POST request as one part of the multipart/form-data request body. As has already been pointed out if you were using the CGI module then all of this would be taken care of for you (there is a whole section in the documentation about uploading files) - you would be ill-advised to try and deal with this yourself as it will require parsing MIME content in the HTTP request body.

    /J\