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);
}
> 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) {
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
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.
>> '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.
bleagh.
operator, could you please connect me to perldoc perlstyle?
-jp
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;
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.
#!/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>);
Sounds like you are on a Windows box and forgot to upload to *nix in
ASCII transfer mode.
Otherwise, please see
perldoc -q 500