Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Full path for uploaded file

by JanetSilk (Initiate)
on Apr 13, 2011 at 10:09 UTC ( [id://899147]=perlquestion: print w/replies, xml ) Need Help??

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

Strictly speaking this may not be a Perl problem, but since it is Perl related I hope someone here can suggest a solution.

I am uploading a file from an HTML form, but when I access the name of the file within my Perl code I don't get the full path name of the uploaded file. For example, if I upload C:\path\file.txt then:

use CGI; use CGI::Carp qw(fatalsToBrowser); $q = new CGI; $filename = $q->param('uploaded_file'); print "Content-type: text/html\n\n"; print "filename = $filename</br>";
will output:

filename = file.txt

So the full path name must be being stripped out by the browser or the server. My question is how can I access the full path name in my Perl code?

Thanks, J.

Replies are listed 'Best First'.
Re: Full path for uploaded file
by afoken (Chancellor) on Apr 13, 2011 at 10:31 UTC

    Browsers are expected to strip the drive and path from an uploaded file, and almost all browsers do so. MSIE is once again broken, it sends the fully qualified filename.

    Why do you think that you need the path of a file? I smell some broken design here.

    If you want to know the path of the file ON THE SERVER after being uploaded, use tmpFileName() as documented.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Thanks Alexander. I wanted to upload a file and process it, and then dynamically create a web page that would allow a user to upload the same file again and process it in some other way. I could just store the file on the server the first time it was uploaded and use it again in the second processing phase, but I don't want to keep a permanent copy of the file on the server, and I don't see any way of ensuring the file gets deleted (the user might decide not to initiate the second processing phase). I thought of using <body onUnLoad="some_script"> but I don't think this works if the user just shuts down the browser instead of navigating away from the page. I also tried using Javascript which gets called when the form's submit button is clicked, but this doesn't work either. I guess I could use a cron job to get rid of the file on the server. Let me know if I have overlooked something. J.
        I wanted to upload a file and process it, and then dynamically create a web page that would allow a user to upload the same file again and process it in some other way.

        Um, what? That sounds like a broken design. Why the two uploads?

        I think you should post some more details. What kind of file do your users upload? What happens during the processing phases?

        I could just store the file on the server the first time it was uploaded and use it again in the second processing phase, but I don't want to keep a permanent copy of the file on the server, and I don't see any way of ensuring the file gets deleted (the user might decide not to initiate the second processing phase).

        Ask the user before uploading starts. Maybe a simple checkbox "run phase 2" is sufficient. Then run either only phase 1 or both phases, depending on the checkbox.

        When you use CGI, CGI.pm will usually take care of cleaning up. Sometimes, when your CGI script crashes, CGI will not clean up, and uploaded files will pile up in some temp directory. The private tempfiles pragma is very helpful when running on systems derived from Unix (Linux, *BSD, ...): It unlinks the temp file, but keeps an open handle for it. The file content is preserved, but as soon as the handle is closed, the operating system will free the disk space occupied by the file. The only disadvantage may be that the file no longer has a name that you could pass to external programs. But you can inherit the handle to an external program, or you can use the handle as STDIN for an external program.

        I thought of using <body onUnLoad="some_script"> but I don't think this works if the user just shuts down the browser instead of navigating away from the page.

        Correct. onUnload is not reliable. Think about using a session, delete the file after the session has expired.

        I also tried using Javascript which gets called when the form's submit button is clicked, but this doesn't work either.

        Define "does not work". And how is submitting a form related to deleting a file on the server?

        I guess I could use a cron job to get rid of the file on the server.

        Yes, but not really needed. You can simply check if you need to clean up during some later request. Either search for expired sessions and clean up their temp files, or simply delete all temp files that are too old to be in use (e.g. older than one day).

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      MSIE is once again broken, it sends the fully qualified filename.

      MSIE 6 and before sends the filename for sure

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://899147]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-28 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found