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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Upload - Change File Name 1

Status
Not open for further replies.

skosterow

Technical User
Feb 23, 2002
135
US
Okay along the same lines - as mu last question. Now i need to change the file name upon UPLOAD.

for example:

if the file name the user selects in the form tag QUESTION-IMAGE is say example01.gif - while using the following code:

I need to rename the file upon upload to: $NEW::FILENAME. Wich in this example we will say is $NEW::FILENAME = "123456";

1: my $filename = $query->param('QUESTION-IMAGE');
2: my $upload_dir = "images/senario";
3: $filename =~ s/.*[\/\\](.*)/$1/;
4: my $upload_filehandle = $query->upload("QUESTION-IMAGE");
5:
6: open UPLOADFILE, ">$upload_dir/$filename";
7: binmode UPLOADFILE; # File IS binnary
8: while ( <$upload_filehandle> )
9: { print UPLOADFILE; }
10: close UPLOADFILE;
11: print $query->header ( );

Ive tried changing line 3 to $filename = $NEW::FILENAME but it dosnt seem to work.

Thanks in advance.

- Scott


Wise men ask questions, only fools keep quite.
 
The only reason you would name a variable like $NEW::FILENAME is in OO perl when you want to globalize a variable within the class to be accessed directly.

Use a name like $new_filename;



 
Do you want to change the variable's name or
you want to rename the file that the variable contains?
 
just use your existing code then at the end rename the file:

my $filename = $query->param('QUESTION-IMAGE');
my $upload_dir = "images/senario";
$filename =~ s/.*[\/\\](.*)/$1/;
my $new_name = '12345.gif';
my $upload_filehandle = $query->upload("QUESTION-IMAGE");

open UPLOADFILE, ">$upload_dir/$filename";
binmode UPLOADFILE; # File IS binnary
while ( <$upload_filehandle> )
{ print UPLOADFILE; }
close UPLOADFILE;
rename("$upload_dir/$filename","$upload_dir/$new_name")
print $query->header();


of course you may need to dynamically grab the file extension (.gif) when the filename is recieved from your form and place that at the end of your new filename instead of just blindly adding an extension like I did in my example above. You can also use the rename () function to move the file to a different directory:

rename("$upload_dir/$filename","$new_dirctory/$new_name");

and then delete the original if need be:

unlink("$upload_dir/$filename");

you should always check that all system operations (open, rename,unlink, etc) are succesful by testing with die or other method:

unlink("$upload_dir/$filename") or die "Unable to delete file: $!";
 
KevinADC -

thanks for pointing me in the right direction!!!!

here is the finished code for anyone that ever searches this FAQ!

if ($QUESTION::IMAGE ne '') {
my $filename = $query->param('QUESTION-IMAGE');
my $upload_dir = "images/senario";
$filename =~ s/.*[\/\\](.*)/$1/;
my $upload_filehandle = $query->upload("QUESTION-IMAGE");

# Split the Current File Name
@split_data = split(/\./,lc($filename));
$filename = $QUESTION::QUESTIONID.".".@split_data[1];


open UPLOADFILE, ">$upload_dir/$filename";
binmode UPLOADFILE; # File IS binnary
while ( <$upload_filehandle> )
{ print UPLOADFILE; }
close UPLOADFILE;
print $query->header ( );

:)

Wise men ask questions, only fools keep quite.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top