in reply to Re: Uploading files (total newbie needs help...)
in thread Uploading files (total newbie needs help...)

Thanks for your answers.
Well, I'm a very unskilled Perl-er, and my problem is not how can I run my programm, but how can I upload the input file in a temp file.

As for the CGI.pm, there are hints into it on how to create an HTML form to upload a file, but what about how to update this very file?

Here is how my starting page looks like:
<p><b><i>Step 1 of 3: Select input file</i></b></p> <form action="http://path/to/my/script/step1.pl" method="POST" enctype +='multipart/form-data'> <table> <tr> <td><font face="Arial" size="2">Select input file:</td> <td><input type="file" name="inputfile" size='30'></td> (...) <div align="right"><input type="submit" value="Next >"></div> </form></font>
My script step1.pl looks like this:
#! perl.exe use strict; use CGI; my $q = new CGI; my $file = $q->param('inputfile'));
and so on. What should I do now with $file?

Thanks

Replies are listed 'Best First'.
Re: Re: Re: Uploading files (total newbie needs help...)
by davorg (Chancellor) on Sep 17, 2001 at 19:03 UTC
    As for the CGI.pm, there are hints into it on how to create an HTML form to upload a file, but what about how to update this very file?

    Keep reading. It's all there in the docs.


    When the form is processed, you can retrieve the entered filename by calling param():

    $filename = $query->param('uploaded_file');

    Different browsers will return slightly different things for the name. Some browsers return the filename only. Others return the full path to the file, using the path conventions of the user's machine. Regardless, the name returned is always the name of the file on the user's machine, and is unrelated to the name of the temporary file that CGI.pm creates during upload spooling (see below).

    The filename returned is also a file handle. You can read the contents of the file using standard Perl file reading calls:

    # Read a text file and print it out while (<$filename>) { print; } # Copy a binary file to somewhere safe open (OUTFILE,">>/usr/local/web/users/feedback"); while ($bytesread=read($filename,$buffer,1024)) { print OUTFILE $buffer; }

    However, there are problems with the dual nature of the upload fields. If you use strict, then Perl will complain when you try to use a string as a filehandle. You can get around this by placing the file reading code in a block containing the no strict pragma. More seriously, it is possible for the remote user to type garbage into the upload field, in which case what you get from param() is not a filehandle at all, but a string.

    To be safe, use the upload() function (new in version 2.47). When called with the name of an upload field, upload() returns a filehandle, or undef if the parameter is not a valid filehandle.

    $fh = $query->upload('uploaded_file'); while (<$fh>) { print; }

    This is the recommended idiom.

    When a file is uploaded the browser usually sends along some information along with it in the format of headers. The information usually includes the MIME content type. Future browsers may send other information as well (such as modification date and size). To retrieve this information, call uploadInfo(). It returns a reference to an associative array containing all the document headers.

    $filename = $query->param('uploaded_file'); $type = $query->uploadInfo($filename)->{'Content-Type'}; unless ($type eq 'text/html') { die "HTML FILES ONLY!"; }
    --
    <http://www.dave.org.uk>

    Perl Training in the UK <http://www.iterative-software.com>

      Hi and thanks for your help
      In the mean time, I have found some useful modules to handle uploads. I also have found out how to use the open(HANDLE, "> /cgi-bin/test.dat"); statement.

      Obviously, the problem I am now facing is that my server runs under NT (alas) and I am having trouble with file paths. Namely:
      If I use the following statement: open(HANDLE, "> test.dat"); my new file test.dat is created on the root of the server where my perl script is running. This is not very nice, so I'm trying to put this file in a more usable location. For instance by using open(HANDLE, "> /cgi-bin/my_programm_folder/test.dat");

      But it won't work. I have tried with backslashes, slashes, dots, double slashes, drive names, full paths, relative paths, it doesn't work. I come from the UNIX (and previously Apple) world and I don't know Windows paths very well, but what I have tried is supposed to be right!

      What am I doing wrong?
Re: Re: Re: Uploading files (total newbie needs help...)
by rob_au (Abbot) on Sep 17, 2001 at 18:32 UTC
    Here is a snippet of code that should do just as you ask - You will obviously need to cater it to your needs, but should give you a starting point:

    #!/usr/bin/perl use CGI; use IO::File; use POSIX; use strict; my $cgi = CGI->new; # temporary file name is supplied by POSIX::tmpnam function my $buffer; my $tmpfile = POSIX::tmpnam; # loop through CGI handle and write new temporary file my $fh = IO::File->new(">".$tmpfile); print $fh $buffer while (read($cgi->param('inputfile'), $buffer, 1024) +); $fh->close; # retrieve file name from CGI file field parameter my $newfile = ($cgi->param('inputfile') =~ /^((?:.*[:\\\/])?)(.*)/s)[1 +]; # rename file unless file exists rename $tmpfile, $newfile unless -e $newfile; # return HTML headers and page data . . exit 0;

     

    Ooohhh, Rob no beer function well without!