Renaming an Uploaded Picture File

D

Doug H

I am a fairly new Perl programmer so am hoping that my problem is just a
simple
mistake that someone can easily help me with.

I have a short Perl script that gets some information from a form on a web
page
and then uploads a picture file to the web site. This part works fine. My
problem
occurs when I try to rename the file that was just uploaded. My coding is as
follows:

#!/usr/local/bin/perl -wT
use CGI;
#get info about file to upload
$upload_dir = "/d4/d8/pscc.shawbiz.ca/html/";
$query = new CGI; $filename = $query->param("uploadfile");
#get other information to process
$picnum= $query->param("picnum");
$pcomm= $query->param("pcomm");
$wpage= $query->param("wpage");
#do the uploading
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("uploadfile");
open UPLOADFILE, ">$upload_dir/$filename";
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
#rename the uploaded file
my $newfilename="/d4/d8/pscc.shawbiz.ca/html/bunnysredone.jpg";
rename ("/d4/d8/pscc.shawbiz.ca/html/bunnys.jpg",$newfilename);
.....
etc (the rest just shows a web page showing the results)

In the above the renaming of the uploaded file works. It renames
the picture file from 'bunnys.jpg' to 'bunnysredone.jpg'. What I
need, though, is to have the new picture name created from
the picture number that was uploaded (the value of $picnum). So
I replaced the line for $newfilename to be

my $newfilename="/d4/d8/pscc.shawbiz.ca/html/pic".$picnum.".jpg";

With this change, the renaming no longer works. If I comment out
the actual rename line of code and display the value of $newfilename
in the output, it shows exactly what I would have expected it to
be
eg
/d4/d8/pscc.shawbiz.ca/html/pic5.jpg

where the 5 represents the current value for $picnum.

I do not see what I am doing wrong. Can anyone help? When I get
this working I would also like to use the same technique to set
the value of the old name in the rename line.

Thank you.
 
J

Jürgen Exner

Doug H said:
my $newfilename="/d4/d8/pscc.shawbiz.ca/html/bunnysredone.jpg";
rename ("/d4/d8/pscc.shawbiz.ca/html/bunnys.jpg",$newfilename);
....
In the above the renaming of the uploaded file works. It renames
the picture file from 'bunnys.jpg' to 'bunnysredone.jpg'.
Ok.

What I
need, though, is to have the new picture name created from
the picture number that was uploaded (the value of $picnum). So
I replaced the line for $newfilename to be

my $newfilename="/d4/d8/pscc.shawbiz.ca/html/pic".$picnum.".jpg";

Yikes! This is Perl, not C. No need for string concatenation:
my $newfilename="/d4/d8/pscc.shawbiz.ca/html/pic$picnum.jpg";
With this change, the renaming no longer works. If I comment out

Why don't you ask perl to help you?

rename (....) or
die "Cannot rename file '$oldfilename' to '$newfilename': $!";

You may also want to re-check the documentation for rename(), as it does
have its OS-specific quirks. File::Copy might be a better solution.

jue
 
D

Doug H

Thanks for the reply, jue.

I removed the concatenation (although they were in the text book I have been
using about Perl) in the line

my $newfilename="/d4/d8/pscc.shawbiz.ca/html/pic".$picnum.".jpg";

to get

my $newfilename="/d4/d8/pscc.shawbiz.ca/html/pic$picnum.jpg";

The rename would still not work but when I again commented out the rename
statement, the value of $newfilename showed correctly. I then added the "or
die "Cannot rename the file '$oldfilename' to '$newfilename': $!"; to the
rename statement. It did not work.

In both cases when I say 'not work', I mean that I get a "Internal Server
Error" message when I try to run the program rather than a web page
generated by my Perl script. Can I assume that this means that the version
of Perl provided by my Internet service provider does not handle what I am
trying to do? Did I leave something out, perhaps in the "use" statement?

Any ideas?

Thanks you again.



Doug H said:
I am a fairly new Perl programmer so am hoping that my problem is just a
simple
mistake that someone can easily help me with.

I have a short Perl script that gets some information from a form on a web
page
and then uploads a picture file to the web site. This part works fine. My
problem
occurs when I try to rename the file that was just uploaded. My coding is
as
follows:

#!/usr/local/bin/perl -wT
use CGI;
#get info about file to upload
$upload_dir = "/d4/d8/pscc.shawbiz.ca/html/";
$query = new CGI; $filename = $query->param("uploadfile");
#get other information to process
$picnum= $query->param("picnum");
$pcomm= $query->param("pcomm");
$wpage= $query->param("wpage");
#do the uploading
$filename =~ s/.*[\/\\](.*)/$1/;
$upload_filehandle = $query->upload("uploadfile");
open UPLOADFILE, ">$upload_dir/$filename";
while ( <$upload_filehandle> )
{
print UPLOADFILE;
}
close UPLOADFILE;
#rename the uploaded file
my $newfilename="/d4/d8/pscc.shawbiz.ca/html/bunnysredone.jpg";
rename ("/d4/d8/pscc.shawbiz.ca/html/bunnys.jpg",$newfilename);
....
etc (the rest just shows a web page showing the results)

In the above the renaming of the uploaded file works. It renames
the picture file from 'bunnys.jpg' to 'bunnysredone.jpg'. What I
need, though, is to have the new picture name created from
the picture number that was uploaded (the value of $picnum). So
I replaced the line for $newfilename to be

my $newfilename="/d4/d8/pscc.shawbiz.ca/html/pic".$picnum.".jpg";

With this change, the renaming no longer works. If I comment out
the actual rename line of code and display the value of $newfilename
in the output, it shows exactly what I would have expected it to
be
eg
/d4/d8/pscc.shawbiz.ca/html/pic5.jpg

where the 5 represents the current value for $picnum.

I do not see what I am doing wrong. Can anyone help? When I get
this working I would also like to use the same technique to set
the value of the old name in the rename line.

Thank you.
 
J

Jürgen Exner

Doug H said:
In both cases when I say 'not work', I mean that I get a "Internal Server
Error" message when I try to run the program rather than a web page
generated by my Perl script.

Dahhh, would have been nice to know this tidbit of information from the
beginning, please see "perldoc -q 500".

Oh, and did you check the server log for the message that was generated
by the die() statement?

jue
 
D

Doug H

Thanks to both repliers.

After refering to my ISP web pages about using Perl I discovered that one
needs to use

use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

to have errors display. I tried this and it worked--I got statement errors
in the script I was trying. However I could not figure out what the errors
actually meant. The statements looked okay to me. Then I had to go to do
something else and then this evening when I got back to trying it I could
not even get these error messages to show! Very discouraging. I do not even
know whether using the above line means that I do not have to use the "use
CGI;" line as well.

In any case I think I will just have to start from scratch and try to get
something to run and show errors if I have them in statements.

You said
Dahhh, would have been nice to know this tidbit of information from the
beginning, please see "perldoc -q 500".

How does one refer to "perldoc -q 500"? Sorry. I have programmed in octal
(in 1958 before there were languages), Fortran, Basic, Visual Basic, COBOL,
PL1 but am new to Perl and the book I have been using to learn it, seems to
disagree with some of the coding I find on the web. It makes it all very
exasperating.

Doug
 
J

Jürgen Exner

Doug H said:
After refering to my ISP web pages about using Perl I discovered that one
needs to use

use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

to have errors display.

That is not quite correct. Normally error messages will be sent to
STDERR and thus be displayed on the terminal together with STDOUT.
However because in a web server environment there is no terminal
anything sent to STDERR is being redirect to the error log where it can
be examined later at your convenience.

The above import shortcuts that method and instead -besides other
things- redirects the error messages from STDERR to STDOUT, thus
including them in the regular response from the CGI program to the web
server.
[...] I could not even get these error messages to show!

Did you check the web server's error log?
Very discouraging. I do not even
know whether using the above line means that I do not have to use the "use
CGI;" line as well.

CGI and CGI::Carp are two totally different modules.
How does one refer to "perldoc -q 500"? Sorry. I have programmed in octal

perldoc is _the_ standard reference for any Perl command, function,
tool, module, you name it. It is automatically installed as part of any
(correctly) installed Perl installation and you just call it.

Use
perldoc perldoc
to find out more about the program itself and
perldoc perl
for a top level overview of what information is available. Some helpful
option are
perltoc: display table of content
-q : search the FAQ
-f : display the documentation of a specific function
ModuleName: display the documentation for module ModuleName
perlop: display list and documentation of all operators in Perl
perlsyn: syntax definition for Perl

There are numerous other documents available, like a reference tutorial,
reference documentation, several OO manuals, regular expression tutorial
and manual, and and and.

As for "perldoc -q 500" just type that command in at your command line.
am new to Perl and the book I have been using to learn it, seems to
disagree with some of the coding I find on the web. It makes it all very
exasperating.

There are quite a few poorly written books and many, many examples of
really bad code out there. If you really want to know how something is
supposed to work then check out perldoc. It is the ultimate although not
always easiest to read reference. And it is always up to date and always
matching the version of Perl that you installed.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top