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

Okay. I have a text box set up using INPUT TYPE=file and NAME="INPUT_FILE" When I click on the Browse... button the following code is run :
$file = $query->param('INPUT_FILE'); print $file;
All that displays is the file but not the directory structure. Meaning if /home/directory/file.doc is in the txt box then all that prints is file.doc can anyone tell me why the /home/directory/ is being cut off

Replies are listed 'Best First'.
RE: grabbing text
by neshura (Chaplain) on May 25, 2000 at 00:44 UTC
    You should read the latest documention on using file uploads with CGI.pm. You can find it here. It explains a lot of these things, including why sometimes the full path shows and sometimes just the filename.

    e-mail neshura

RE: grabbing text
by lhoward (Vicar) on May 25, 2000 at 00:45 UTC
    From the CGI.pm docs:
    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 I<user's> machine, and is unrelated to the name of the temporary file that CGI.pm creates during upload spooling (see below).
    You don't need to know the directory in your cgi because (if you do everything right) the file contents are transmitted to your .cgi along with any other fields in the form when the users does the form submit. The server does not open a separate connection back to the web browser to get the file.
      If you didn't strip this, I bet most people will end up with something like:

      $ ls
      hello.txt
      c:\upload\foo.doc
      c:\temp\send.txt
      etc...
      
      :).. this is how CGI.pm gets the file name:

      my($filename) = $header{'Content-Disposition'}=~/ filename="?([^\";]*)"?/;

      lhoward did point out that some clients do send the full path. I need to look deeper into the CGI.pm source when I have an extra tuit.

      Cheers,
      KM

RE: grabbing text
by cei (Monk) on May 25, 2000 at 04:34 UTC
    INPUT TYPE=file is not a text box at all. Sure it SHOWS a text box, but the contents of the text box are not what's passed to the CGI. The FILE with the name listed in the text box is sent. If all you're trying to do is deal with a file name, but not the file itself, you're using the wrong tool.
Re: grabbing text
by KM (Priest) on May 25, 2000 at 00:30 UTC
    When you submit a form to upload a file, the script has been sent a file, by it's name, not by it's path. The path info is only so the client knows where to get the desired file.

    Cheers,
    KM

      In that case then if I need to open that file that was in the textbox in order to parse through it...how can I find the file if I cannot get the path info.
        You need to upload the file to get the data. The client knows where the file is, the program doesn't need to. When I say 'upload' I don't mean that it has to be turned into a physical file on disk, but you have to read the data in. Look at the CGI.pm module, which will send you on the path to getting this done.

        Cheers,
        KM

RE: grabbing text
by Anonymous Monk on May 25, 2000 at 21:04 UTC
    Macs do this by default. Are you using a mac?
Re: grabbing text
by princepawn (Parson) on May 25, 2000 at 17:38 UTC
    May I suggest you look at my module HTTP::File? It allows for one-line architecture independant file uploads via CGI.pm. Available on CPAN, author ID TBONE