Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Uploaded File Empty but in correct folder with correct name

3 views
Skip to first unread message

froil

unread,
Feb 27, 2006, 9:59:07 PM2/27/06
to
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);
}

Samwyse

unread,
Feb 28, 2006, 12:29:03 AM2/28/06
to
froil wrote:
> 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) {

Gunnar Hjalmarsson

unread,
Feb 28, 2006, 2:32:05 AM2/28/06
to
Samwyse wrote:
> froil wrote:
>> 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:"

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Gunnar Hjalmarsson

unread,
Feb 28, 2006, 2:32:08 AM2/28/06
to
froil wrote:
> When i run the script below the output looks good, but the file that is
> uploaded is 0 bytes.

I see that you keep struggling with your upload script.

http://groups.google.com/group/comp.lang.perl.misc/browse_frm/thread/3f69b62c8c76d5a4

Preventing problems like the one you are experiencing was the reason I
wrote the CPAN module CGI::UploadEasy.

Samwyse

unread,
Feb 28, 2006, 8:19:49 AM2/28/06
to
Gunnar Hjalmarsson wrote:
> Samwyse wrote:

>> '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:"

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.

froil

unread,
Feb 28, 2006, 10:57:20 PM2/28/06
to
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;
}

DJ Stunks

unread,
Feb 28, 2006, 11:03:09 PM2/28/06
to

bleagh.

operator, could you please connect me to perldoc perlstyle?

-jp

Gunnar Hjalmarsson

unread,
Mar 1, 2006, 2:30:58 AM3/1/06
to
froil wrote:
> 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;

froil

unread,
Mar 1, 2006, 6:14:47 PM3/1/06
to
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;

Gunnar Hjalmarsson

unread,
Mar 1, 2006, 6:53:13 PM3/1/06
to
froil wrote:
> 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.

froil

unread,
Mar 1, 2006, 7:47:20 PM3/1/06
to
Thanks Gunner it is working so far!!! You are great.

froil

unread,
Mar 1, 2006, 11:27:51 PM3/1/06
to
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:jilesfr_...@69.6.255.192: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>);

Gunnar Hjalmarsson

unread,
Mar 2, 2006, 8:21:47 AM3/2/06
to
froil wrote:
> 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

0 new messages