Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Renaming an Uploaded Picture File

Reply
Thread Tools

Renaming an Uploaded Picture File

 
 
Doug H
Guest
Posts: n/a
 
      03-13-2010
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.


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      03-13-2010
"Doug H" <> wrote:
[...]
>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
 
Reply With Quote
 
 
 
 
Doug H
Guest
Posts: n/a
 
      03-14-2010
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" <> wrote in message
news:53Smn.32025$...
>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.
>
>



 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-14-2010
"Doug H" <> wrote:
>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
 
Reply With Quote
 
Doug H
Guest
Posts: n/a
 
      03-15-2010
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


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      03-15-2010
"Doug H" <> wrote:
>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.

>> 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


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Renaming picture in folders... vincenzo.bacarella@gmail.com Perl Misc 5 01-04-2006 07:32 PM
Determine the File Type of an Uploaded File Spotted Owl Eater ASP .Net 1 11-16-2005 05:00 PM
Uploaded File Storage - Database vs File System Arsen V. ASP .Net 9 02-11-2005 04:48 AM
overlay text on uploaded picture Brian Lowe ASP .Net 5 06-24-2004 06:13 PM
Virus-Scanning uploaded files uploaded? Matt G ASP .Net 1 08-22-2003 05:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57