Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Uploading Multiple Files to Website

Status
Not open for further replies.

senorsquiid

IS-IT--Management
Jul 7, 2006
1
0
0
US
I've written a script to upload and rename 2 files (jpegs) to our company website, and I've enver worked with using File::Copy on multiple files before, and the rename feature might be wack too. It's posted below, so does anyone have any suggestions?

Thanks.

#!/usr/bin/perl -w

use strict;
use CGI;
use File::Copy;
my $q = new CGI();
my $adid = $q->param("advertid");
my $File = $q->param("filex");
my $File2 = $q->param("filez");
my $Pic = $q->tmpFileName($File);
my $Pic2 = $q->tmpFileName($File2);
copy($Pic,"/home/websites/papacks.com/htdocs/Images/Cards/$File");
copy($Pic2,"/home/websites/papacks.com/htdocs/Images/Cards/$File2");
rename "$File","$adid_front";
rename "$File2","$adidID_back";
print "What a cheezy victory for you fnord!\n";
 
Normally, an upload script receives the binary data of the uploaded file but doesn't save it anywhere unless you specifically tell it to. Why not just save it under the correct name in the first place rather than save it and then copy and rename it elsewhere?

Code:
my $adid = $q->param ("advertid");
my $File = $q->param ("filex");
my $File2 = $q->param ("filez");

# Get filehandles for the uploads.
my $Upload = $q->upload ("filex");
my $Upload2 = $q->upload ("filez");

# Read each upload.
my ($Binary,$Binary2) = ('','');
while (<$Upload>) {
   $Binary .= $_;
}
while (<$Upload2>) {
   $Binary2 .= $_;
}

# Save each file to their final filename.
open (FRONT, ">/home/websites/papacks.com/htdocs/Images/Cards/$adid\_front");
binmode FRONT;
print FRONT $Binary;
close (FRONT);

open (BACK, ">/home/websites/papacks.com/htdocs/Images/Cards/$adid\_back");
binmode BACK;
print BACK $Binary;
close (BACK);
 
Furthermore, as I was replying I noticed another problem with your code:

Code:
rename "$File","$adid_front";
rename "$File2","$adidID_back";

You'll need to escape the _ or concatenate it, because $adid_front and $adidID_back are two valid names for variables. You'd want to do:

Code:
rename "$File","$adid\_front";
rename "$File2","$adidID\_back";

as I did in the code I just posted above.
 
This is a perfect example of why it's essential to use the `strict' pragma when writing all but the most trivial programs. It would have picked up on that error straight away, by complaining that the $adid_front and $adid_back variables hadn't previously been declared.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top