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

I am trying to use the following script to upload files from a WinXP client to a Win32 server.

#!/local/bin/perl use CGI; my $cgi = new CGI; my $file = $cgi->param('uploadfile'); $file=~m/^.*(\\|\/)(.*)/; # strip the remote path and keep the fil +ename my $name = $2; open(LOCAL, ">$name") or die $!; while(<$file>) { print LOCAL $_; } print $cgi->header(); print "$file has been successfully uploaded... thank you.\n";

The problem I'm having is that when the local file on the WinXP client is written to the server it is always 0 (zero) length i.e. the filename gets created but no data gets written to the remote file. I've tried a similiar script from the PERL Cookbook using sysopen and I get the same result.

Not sure what's going on but any help/advice would be much appreciated.

Thanks

Eoin

Replies are listed 'Best First'.
Re: Uploading Files (Win32)
by helgi (Hermit) on Feb 06, 2003 at 14:13 UTC
    Your upload method is wrong. $file will only contain the filename and not upload anything.

    I suggest reading the documentation for the CGI module (perldoc -X CGI), specifically the chapter entitled:
    CREATING A FILE UPLOAD FIELD

    Here is some code, mostly from the documentation to start you off. I haven't time to do all of it now. Please specify a full path for where you want to save the file locally. The browser does not and should not have permissions to write wherever he pleases on the server.

    #!perl -wT use strict; use CGI; my $form = new CGI; print $form->header; my $filehhandle = $form->upload('FILE'); print "<br>Contents of file:<br><pre>"; while (<$filehandle>){ print;} print "</pre></body></html>";
    --
    Regards,
    Helgi Briem
    helgi AT decode DOT is

      I have changed my code to the following and I am still getting the same result i.e. the contents of the file are not read! Here is the code I am using:

      use CGI qw/:all/; my $form = new CGI; print $form->header; my $filehandle = $form->upload('UPLOADFILE'); print "<br>Contents of file:<br><pre>"; while (<$filehandle>){ print ;} print "</pre></body></html>";

      I'm very new to PERL but the problem seems to be that the filehandle for the local file (i.e. the file to be uploaded) is not pointing to the local file for some strange reason. If I use the 'open' command to read the contents of the local file then I get an error saying "no such file or directly". Is this problem something to do with paths and forward and back slashes>

      Any more help would be appreciated

      Eoin

        Is this problem something to do with paths and forward and back slashes?

        Almost certainly.

        Use the full path.

        Use forward slashes / There is NO need to use backward slashes ever under Windows unless you specifically want to work through the cmd.exe or command.com shells (you don't).

        Your current working directory os almost certainly NOT what you think it is which is probably causing the problem.

        You don't show how you are opening the external file for writing. Try something like (untested):

        my $dir = 'C:/temp/web'; # or something like that my $localfile = "$dir/$filename"; open OUT, ">", $localfile or die "Cannot open $localfile for writing:$ +!\n";
        --
        Regards,
        Helgi Briem
        helgi AT decode DOT is
Re: Uploading Files (Win32)
by Heidegger (Hermit) on Feb 06, 2003 at 15:38 UTC

    Check if the following two things are valid in your script:

    1. Your generated HTML form source must be multipart: <form ENCTYPE="multipart/form-data" etc.

    2. If you're uploading a binary file, call the binmode(OUTFILE); function (example):

    open (OUTFILE, ">>$file_path"); binmode(OUTFILE); while($bytesread=read($file_name, $buffer, 1024)) { print OUTFILE $buffer; }
Re: Uploading Files (Win32)
by Heidegger (Hermit) on Feb 06, 2003 at 16:28 UTC
    The problem I'm having is that when the local file on the WinXP client is written to the server it is always 0 (zero) length i.e. the filename gets created but no data gets written to the remote file.
    So the file is created, it's just that it's zero length after upload. This means there are no problems with paths/directories. I think the Anonymous Monk forgot to specify that it's the multipart form. I forgot about this a few days ago myself ;-)

      Heidegger,

      Thanks for your response. Here is the HTML code for the form.

      <form action="filemngt_upload_file.pl" method="get" enctype="multipart +/form-data"> <strong>File to uploaded?</strong> <input type="file" name="UPLOADFILE" size="30"> <input type="submit" value="Upload File"> <input type="reset" value="Clear"> </form>

      I think the above is OK. You might notice I am using 'get' instead of 'post'. This is because 'post' causes my program to lock on the server - ISP has been unable to offer an explanation as yet!.

      Thanks

      Eoin

        You emphatically CANNOT use get with a multipart form, especially one that contains a file upload, you must use post.