PDA

View Full Version : Image upload script (variable path)


tdavis
08-23-2006, 07:09 PM
I have this script that uploads photos (thanks to mlseim). It works perfectly. But, I have tried to modify it so that the directory is variable. I have a text field where I enter the variable name. That works too. But, I cannot take it to the next level, which is a sub-directory in that variable named directory.

If I replace the line below that does not work with this, it works.
my $upload_dir = "../gallery/" . $dir;

This seems strange to me, but maybe I am overlooking something simple. Any help is appreciated. Thanks, -tdavis.


#!/usr/local/bin/perl
use CGI qw(:standard);
use strict;
my $cgi = new CGI;
my $max_files = 10; #maximum number of files allowed.
my $dir = $cgi->param("directory_name");
my $type = $cgi->param("pictureType");
my $upload_dir = "../gallery/" . $dir . "/thumbnails"; Does not work
print $cgi->header(-type=>'text/html');
for (my $count=1; $count<=$max_files; $count++){
my $filen = "photo".$count;
my $file = $cgi->param($filen);
my $filename = $file;
$filename =~ s/^.*(\\|\/)//g;
if($file){
open(OUT, ">$upload_dir/$filename") || die print "Fail to upload: $!";
while(<$file>) {
print OUT;
}
close(OUT);
}
}
print "File(s) Uploaded\n";

FishMonger
08-23-2006, 07:35 PM
What is the error message in the web server error log?
Does the desired upload directory exist? If so, does it have the proper permissions?
Have you tried using Data::Dumper to output the value of $dir to see if it has is a trailing slash?

tdavis
08-23-2006, 07:49 PM
No errors, but only this in the error_log:
1 at /opt2/tdavisco/httpd/htdocs/lifetoimage/admin/file_upload.cgi line 21.

The script dies, and displays the following, which it is written to do:
Fail to upload: No such file or directory

The sub-directory, "thumbnails", is definitly there.
I checked that many times (thought I might be going blind)...

KevinADC
08-23-2006, 07:52 PM
if the directory does not exist you have to create it first. Using unvalidated data in a directory path is not a good idea and you should be using taint mode with that code.

There is nothing technically wrong with the offending line of code:
my $upload_dir = "../gallery/" . $dir . "/thumbnails"; Does not work

but it could be written simpler:

my $upload_dir = "../gallery/$dir/thumbnails";

this line should be changed:

open(OUT, ">$upload_dir/$filename") || die print "Fail to upload: $!";

change to:

open(OUT, ">$upload_dir/$filename") or die "Fail to upload: $!";

now you should see the error message in the browser.

FishMonger
08-23-2006, 08:01 PM
Try running it with the addition of these debugging statements and see what happens.

#!/usr/local/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use Data::Dumper;
use strict;
my $cgi = new CGI;
my $max_files = 10; #maximum number of files allowed.
my $dir = $cgi->param("directory_name");
my $type = $cgi->param("pictureType");
my $upload_dir = "../gallery/" . $dir . "/thumbnails"; # Does not work
print $cgi->header(-type=>'text/html');
# depending on your browser, you may need to "view source" to see the warning messages
warningsToBrowser(1);
print Dumper $upload_dir;
for (my $count=1; $count<=$max_files; $count++){
my $filen = "photo".$count;
my $file = $cgi->param($filen);
my $filename = $file;
$filename =~ s/^.*(\\|\/)//g;
if($file){
open(OUT, ">$upload_dir/$filename") || die print "Fail to upload: $!";
while(<$file>) {
print OUT;
}
close(OUT);
}
}
print "File(s) Uploaded\n";

tdavis
08-23-2006, 08:11 PM
The error did display in the browser OK. And the concatenation now seems to work; that is, your suggestion to do this my $upload_dir = "../gallery/$dir/thumbnails"; resolved the problem. The image loaded into the correct directory. So thanks for that!

I would like to ask one more thing. I added this to the script, to see if I can move it to one of two sub-directories (based on a radio button selection). Now I get these messages in the browser (thanks FishMonger!):

Added this:
if ($type eq "thumbnail") {
my $upload_dir = "../gallery/$dir/thumbnail";
}
if ($type eq "fullsize") {
my $upload_dir = "../gallery/$dir/fullsize";
}

I get these errors:
Global symbol "$upload_dir" requires explicit package name at /opt2/tdavisco/httpd/htdocs/lifetoimage/admin/file_upload.cgi line 23.

Thanks for your help!
-tdavis

KevinADC
08-23-2006, 08:16 PM
use the real full path instead of ../ and you will probably be OK.

my $upload_dir = "path/from/root/gallery/$dir/thumbnail";

FishMonger
08-23-2006, 08:22 PM
It's a scoping issue; the declarations in the if statements are private to those blocks. Declare $upload_dir prior to the if statements.

my $upload_dir;
if ($type eq "thumbnail") {
$upload_dir = "../gallery/$dir/thumbnail";
}
if ($type eq "fullsize") {
$upload_dir = "../gallery/$dir/fullsize";
}

tdavis
08-23-2006, 08:22 PM
I got the same error.

What does Global symbol "$upload_dir" requires explicit package name mean?

FishMonger
08-23-2006, 08:26 PM
If those are the only choices of $type, you can simplify it by using a trinary operator.

my $upload_dir = ($type eq "thumbnail") ? "../gallery/$dir/thumbnail" : "../gallery/$dir/fullsize";

tdavis
08-23-2006, 08:29 PM
That did it! All is working!
Thanks very much!

FishMonger
08-23-2006, 08:29 PM
http://search.cpan.org/~nwclark/perl-5.8.8/lib/strict.pm

KevinADC
08-23-2006, 09:50 PM
It's a scoping issue...

ahhh... good catch, I missed that. :o :thumbsup: