Getting the directory of an uploaded file with perl
I have a Perl script which will allow for a file to be uploaded to my server. I'm not completely done with it yet, but I would like to know, if there is any way for me to get the full path of a file i uploaded. Shouldn't this be possible by checking the PATH_INFO environment variable, but when i try to check the path info, nothing is there
#!/usr/bin/perl
use strict;
use CGI;
use File::Basename;
#Virtual Directory
eval
{
my ($uploadfile, $cgi);
$cgi = new CGI;
$uploadfile = $cgi->upload('uploadfile');
print $uploadfile;
use constant PASSWORD => 'p3rlD3v3l0p3r';
use constant UPLOAD_DIR => '/home/user/files';
print <<"HTML";
Status: 200 OK
Content-Type: text/html
<html>
<head>
开发者_开发技巧 <title>Virtual Directory</title>
</head>
<body>
$uploadfile
<h2>Upload a new file</h2>
<form method = "POST" enctype = "multipart/form-data" action =""/>
File:<input type = "file" name="uploadfile"/>
<p>Password:
<input type = "password" name ="passwd"/></p>
<p><input type = "submit" value= "Submit File" /></p>
</form>
</body>
</html>
HTML
print $ENV{"PATH_INFO"};
};
It is hard to make sense of your question. However, do read Processing a File Upload Field in CGI docs. I think you might be looking for:
Accessing the temp files directly
When processing an uploaded file,
CGI.pm
creates a temporary file on your hard disk and passes you a file handle to that file. After you are finished with the file handle,CGI.pm
unlinks (deletes) the temporary file. If you need to you can access the temporary file directly. You can access the temp file for a file upload by passing the file name to thetmpFileName()
method:
On the other hand, if you are trying to find out the full path of the file on the web site visitor's computer, I hope it should be obvious that there is no way for your script to retrieve this information if it is not submitted by the user's browser as the name of the file:
If you want the entered file name for the file, you can just call
param()
:$filename = $q->param('field_name');
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
PATH_INFO is for other purposes. You need "tmpFileName":
my $cgi = CGI->new;
my $filename = $cgi->param('uploadfile');
my $tmpfilename = $cgi->tmpFileName($filename);
You can extract path with "fileparse" from File::Basename.
精彩评论