Prev: FAQ 9.23 How do I find out my hostname, domainname, or IP address?
Next: FAQ 5.14 How can I translate tildes (~) in a filename?
From: Doug H on 13 Mar 2010 14:54 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.
From: J�rgen Exner on 13 Mar 2010 15:39 "Doug H" <courses(a)shaw.ca> 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
From: Doug H on 14 Mar 2010 00:48 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" <courses(a)shaw.ca> wrote in message news:53Smn.32025$sx5.23766(a)newsfe16.iad... >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. > >
From: Tad McClellan on 14 Mar 2010 01:18 [ Please learn how to properly compose a followup message. Please do not top-post. ] Doug H <courses(a)shaw.ca> wrote: > I then added the "or > die "Cannot rename the file '$oldfilename' to '$newfilename': $!"; to the ^^ ^^ The $! variable contains an error message. > 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 Then you should look for the error message that was put into the server's error log. >> #!/usr/local/bin/perl -wT Do you know what the -T switch does? perldoc perlrun describes perl's command line switches. forces "taint" checks to be turned on so you can test them. Ordinarily these checks are done only when running setuid or setgid. It's a good idea to turn them on explicitly for programs that run on behalf of someone else whom you might not necessarily trust, such as CGI programs or any internet servers you might write in Perl. See L<perlsec> for details. -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" The above message is a Usenet post. I don't recall having given anyone permission to use it on a Web site.
From: J�rgen Exner on 14 Mar 2010 03:07
"Doug H" <courses(a)shaw.ca> 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 |