File uploading from a local desktop to a web server..

C

clearguy02

Hi Experts,

I have the below script and I can be able to copy only the file name,
but not the file contents.

=======================================
use warnings;
use Time::Local;
use CGI;
use CGI::Carp;
use CGI qw:)fatalsToBrowser);
use Env;

$query = new CGI;
$attachedFile = $query -> param ("attachedfile");
### capturing the file name from a html page from the local desktop.

$attachedFile=~ s/.*[\/\\](.*)/$1/;

$upload_filehandle = $query -> upload("attachedfile");

$uploaddir = "C:\\Attach\\"; #### on web server..

$temp = $uploaddir . $attachedFile;

open UPLOADFILE, ">$temp";

while (<$upload_filehandle>)
{
print UPLOADFILE;
}
close UPLOADFILE;
===================================================

Where I am going wrong?

Thanks in advance,
JS
 
N

nobull

Hi Experts,

I have the below script and I can be able to copy only the file name,
but not the file contents.

=======================================
use warnings;
use Time::Local;
use CGI;
use CGI::Carp;
use CGI qw:)fatalsToBrowser);
use Env;

$query = new CGI;
$attachedFile = $query -> param ("attachedfile");
### capturing the file name from a html page from the local desktop.

$attachedFile=~ s/.*[\/\\](.*)/$1/;

$upload_filehandle = $query -> upload("attachedfile");

$uploaddir = "C:\\Attach\\"; #### on web server..

$temp = $uploaddir . $attachedFile;

open UPLOADFILE, ">$temp";

while (<$upload_filehandle>)
{
print UPLOADFILE;
}
close UPLOADFILE;
===================================================

Where I am going wrong?

No strictures. Probably not your problem but then again maybe it is.
If you can't be bothered to ask Perl for help why should anyone else be
bothered to see if your problem is one that Perl would have told you
about?

If the file is getting created then open() is probably not failing but
you should get into the habit of always looking to see if it is, and if
so what the reason is.

Do you have any reason to believe the client is properly sending the
file?
Some web browsers (most in my experience) will not tell you if you try
to submit a form with upload fields using a method/encoding that
doesn't support files.

Could also be a permissions issue - it's perfectly possbile on NTFS to
have permission to create files in C:\Attach\ but not to write any data
to them. Try writing some fixed data to the file.
 
P

Paul Lalli

I have the below script and I can be able to copy only the file name,
but not the file contents.

I have no idea what "copy only the file name" means. What is your
expected output, and how do the results differ?
=======================================
use warnings;

Good for using warnings. Very very very Bad for not using strict. Ask
Perl for all the help it can give you.
use Time::Local;
use CGI;
use CGI::Carp;
use CGI qw:)fatalsToBrowser);

You are confusing two very different modules. Those previous two lines
should be the single line:
use CGI::Carp qw:)fatalsToBrowser warningsToBrowser);

And then, after you've printed the HTTP header, do:
warningsToBrowser(1);
use Env;
Why?

$query = new CGI;
$attachedFile = $query -> param ("attachedfile");
### capturing the file name from a html page from the local desktop.

$attachedFile=~ s/.*[\/\\](.*)/$1/;

Ugh.
Use File::Basename;
$attachedFile = basename($attachedFile);
$upload_filehandle = $query -> upload("attachedfile");

$uploaddir = "C:\\Attach\\"; #### on web server..

Perl on windows is perfectly happy to use sane directory separators.
Don't go through silly contortions like this.
my $uploaddir = 'C:/Attach/';
$temp = $uploaddir . $attachedFile;

open UPLOADFILE, ">$temp";

You're not even bothering to determine whether or not the open
succeeded. And because you weren't sending warnings to your browser
either, I'm willing to bet you wouldn't see any "print on closed file
handle" warnings

open my $UPLOADFILE, '>', $temp or die "Could not open $temp: $!\n";
while (<$upload_filehandle>)
{
print UPLOADFILE;
}
close UPLOADFILE;
===================================================

Where I am going wrong?

Take these modifications, make your script strict-compliant, and then
see what results you get. If you're still having problems, let us know
exactly what the problem is.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top