Uploaded File Empty but in correct folder with correct name

F

froil

When i run the script below the output looks good, but the file that is
uploaded is 0 bytes. If anyone can help me find out why the file is
empty that would be awesome.

the script is passed the values of
filename and description through a web form
www.jilesfamily.net/uploadtest.html using the encoding of
multipart/formdata.

#!/usr/bin/perl -wT
#uploadtest.cgi
use strict;
use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);
$CGI::pOST_MAX = 1024 * 1000;
my $upload_dir =
"/hsphere/local/home/jilesfre/jilesfamily.net/uploads";
my $File_Name = param('filename');
if (!$File_Name && cgi_error()) {
print header(-status=>cgi_error());
exit 0;
}
my $description = param('description');
my $New_Name = Get_Full_Name($File_Name);
Store_Results();
Print_Results();
sub Print_Results{
print header;
print start_html('TEST Pictures');
print qq(<h1>Description: $description</h1>);
print qq(<h1> File Name: $File_Name</h1>);
print qq(<h1> New Name: $New_Name</h1>);
print end_html;
}
sub Store_Results{
my $data;
open (STORAGE, ">$upload_dir/$New_Name")
or die "Error: $upload_dir/$New_Name: $!\n";
binmode ($File_Name);
binmode (STORAGE);
while( read($File_Name, $data, 1024) ){
print STORAGE $data;
}
close STORAGE;
}
sub Get_Full_Name{
my $full_name = shift;
$full_name =~ s/.*[\/\\](.*)/$1/;
return($full_name);
}
 
S

Samwyse

froil said:
When i run the script below the output looks good, but the file that is
uploaded is 0 bytes. If anyone can help me find out why the file is
empty that would be awesome.
my $File_Name = param('filename'); [...]
binmode ($File_Name); [...]
while( read($File_Name, $data, 1024) ){

'binmode' and 'read' both want file handles as their only/first
parameter; what you have is the name of a file. I don't remember
exactly how CGI handles files, but you may want to do something similar
to this:
open FH, $File_Name or die;
binmode FH;
while (read FH, $data, 1024) {
 
G

Gunnar Hjalmarsson

Samwyse said:
froil said:
When i run the script below the output looks good, but the file that is
uploaded is 0 bytes. If anyone can help me find out why the file is
empty that would be awesome.

my $File_Name = param('filename');
[...]
binmode ($File_Name);
[...]
while( read($File_Name, $data, 1024) ){

'binmode' and 'read' both want file handles as their only/first
parameter; what you have is the name of a file.

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

$filename = $query->param('uploaded_file');
....
The filename returned is also a file handle. You can read the contents
of the file using standard Perl file reading calls:"
 
S

Samwyse

Gunnar said:
Samwyse wrote:

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

$filename = $query->param('uploaded_file');
...
The filename returned is also a file handle. You can read the contents
of the file using standard Perl file reading calls:"

Well, I *did* say that it had been a while since I did any CGI work.

Next time a CGI question comes in, I'll just keep my mouth shut.
 
F

froil

ok i used uploadeasy and it gave me the error Can't use an undefined
value as a HASH reference at
/usr/lib/perl5/site_perl/5.6.1/CGI/UploadEasy.pm line 209
here is the code... please help
use strict;
use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);
use CGI::UploadEasy;
$CGI::pOST_MAX = 1024 * 1000;
my $query = new CGI;
my $upload_dir =
"/hsphere/local/home/jilesfre/jilesfamily.net/uploads";
my $File_Name = $query->param('filename');
my $description = param('description');
my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);
my $Url_Path = "uploads";
my $File_Name = param('filename');
my $Description = param('description');
my $File = ( keys %{ $ue->fileinfo } )[0];
Print_Results();
sub Print_Results{
print header;
print start_html('TEST Pictures');
print qq(<h1>Description: $description</h1>);
print qq(<h1> File Name: $File_Name</h1>);
print end_html;
}
 
D

DJ Stunks

froil said:
ok i used uploadeasy and it gave me the error Can't use an undefined
value as a HASH reference at
/usr/lib/perl5/site_perl/5.6.1/CGI/UploadEasy.pm line 209
here is the code... please help
use strict;
use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);
use CGI::UploadEasy;
$CGI::pOST_MAX = 1024 * 1000;
my $query = new CGI;
my $upload_dir =
"/hsphere/local/home/jilesfre/jilesfamily.net/uploads";
my $File_Name = $query->param('filename');
my $description = param('description');
my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);
my $Url_Path = "uploads";
my $File_Name = param('filename');
my $Description = param('description');
my $File = ( keys %{ $ue->fileinfo } )[0];
Print_Results();
sub Print_Results{
print header;
print start_html('TEST Pictures');
print qq(<h1>Description: $description</h1>);
print qq(<h1> File Name: $File_Name</h1>);
print end_html;
}

bleagh.

operator, could you please connect me to perldoc perlstyle?

-jp
 
G

Gunnar Hjalmarsson

froil said:
ok i used uploadeasy and it gave me the error Can't use an undefined
value as a HASH reference at
/usr/lib/perl5/site_perl/5.6.1/CGI/UploadEasy.pm line 209

It's because you both create a CGI object and call CGI functions before
the CGI::UploadEasy object has been created. See the CAVEATS section in
the CGI::UploadEasy POD.
$CGI::pOST_MAX = 1024 * 1000;

That line is redundant, since $CGI::pOST_MAX is set by CGI::UploadEasy.
my $query = new CGI;

That line creates a CGI object, and causes your script to fail later.
Remove it.
my $File_Name = $query->param('filename');
my $description = param('description');
my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);

Wrong order. And since you are using CGI.pm's function-oriented style in
the rest of your script, why not do so here as well?

my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);
my $File_Name = param('filename');
my $description = param('description');

Optionally, if you want to use CGI.pm's object-oriented style, the best
way is to do:

my $query = $ue->cgiobject;
 
F

froil

ok i changed the code to what it is below and i am still getting the
same can't use a value as undef hash ref line 209. I really appriciate
all the help you guys have given me.
#!/usr/bin/perl -w
#uploadtest.cgi
use strict;use
CGI::UploadEasy;
use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);
use Data::Dumper;
my $upload_dir =
"/hsphere/local/home/jilesfre/jilesfamily.net/uploads";
my $Description = param('description');
my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);
my $Url_Path = "cgi-bin/pictures";
my $File_Name = param('filename');
my $info = $ue->fileinfo;
print header('text/plain');
print Dumper $info;
 
G

Gunnar Hjalmarsson

froil said:
ok i changed the code to what it is below and i am still getting the
same can't use a value as undef hash ref line 209.

That's because you still call the CGI::param() function before the
creation of the CGI::UploadEasy object.
my $Description = param('description');
my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);

Switch the order of those two lines.
 
F

froil

Ok so now i add a little bit to connect to my database to store the
description, and i get error 500. when i run the syntax check i get no
errors and all the error log says is premature end of script. i can't
find any stupid mistakes, but i am sure there is one.

#!/usr/bin/perl -wT
#uploadtest.cgi
use strict;
use CGI qw:)standard);
use DBI;
use CGI::Carp qw(fatalsToBrowser);
use CGI::UploadEasy;
my $upload_dir =
"/hsphere/local/home/jilesfre/jilesfamily.net/uploads";
my $ue = CGI::UploadEasy->new(-uploaddir => $upload_dir);
my $Url_Path = "cgi-bin/pictures";
my $File_Name = param('filename');
my $info = $ue->fileinfo;
my $Description = param('description');
my $File = ( keys %{ $ue->fileinfo } )[0];
my $username = '?';
my $password = '?';
my $data_source = 'DBI:mysql:[email protected]:3306';
my $DBH = DBI->connect( $data_source, $username, $password )
or die "Error: $DBI::errstr\n";
my $sth_insert =
$DBH->prepare( qq{INSERT INTO files (Description,
FileName) VALUES (?,?)} )
or die "Error: can't connect! $DBH::errstr\n";
$sth_insert->execute($Description,$File);
$DBH->disconnect;
print header;
print start_html('files uploaded');
print qq(<h1>File: $File</h1>);
print qq(<h2>Description: $Description</h2>);
 
G

Gunnar Hjalmarsson

froil said:
Ok so now i add a little bit to connect to my database to store the
description, and i get error 500. when i run the syntax check i get no
errors and all the error log says is premature end of script.

Sounds like you are on a Windows box and forgot to upload to *nix in
ASCII transfer mode.

Otherwise, please see

perldoc -q 500
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top