Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

problem changing name of uploaded file!

by Anonymous Monk
on Sep 03, 2001 at 20:00 UTC ( #109882=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hi all, im in desperate need of help.
im uploading files using cgi.pm and i have 2 things i need some help on:

1. using

my $file = $q->param('uploaded_file') || error($q,"No file received"); my $file_handle = $q->upload($file);

results in $file_handle being empty..in order to get a file handle i have to use:

my $file_handle = $q->upload('uploaded_file');

which is not what the cgi.pm manual sez!?

2. im in a controled environment and id like to change the name of the file uploaded, but simply changing the $file parameter, results in the file being created(with the new name), but nothing is written to it (file size is 0). if i dont change the name of the file, then it works perfectly.(file is saved with the original name,size and type)

how shall i go about to change the name of the file,before i save it to disk ?

any/all help appriciated

kenneth
kennethj@stud.cs.uit.no

Replies are listed 'Best First'.
Re (tilly) 1: problem changing name of uploaded file!
by tilly (Archbishop) on Sep 03, 2001 at 23:25 UTC
    Answers.
    1. This is as documented. For an uploaded file both param and upload return something that can be used as a filehandle. The difference is that someone trying to be mean to you can send "/etc/passwd" as a string, if you make the mistake of trusting param*( you may wind up displaying your own password file. But upload() will not fall for this.
    2. If you take the return from param() and use it as a string you will get the name of the file on the remote system. However the file has actually been spooled to a local temp file, and you are free to open a new file named whatever you want, read from the uploaded filehandle, and print to the new file. On 5.6.0 you should be able to do it something like this (untested):
      my $in = $q->upload('uploaded_file'); my $save_as = "demo.txt"; open(my $out, "> $save_as") or die "Cannot write to '$save_as': $!"; print $out <$in>; close($out);
      Previous to 5.6.0 you couldn't use a scalar there, and would need to use Symbol and gensym a symbol for use.
    Does that help?
Re: problem changing name of uploaded file!
by tachyon (Chancellor) on Sep 03, 2001 at 21:22 UTC

    When you upload a file CGI.pm creates a temporary file to store the contets of the upload. CGI.pm introduced the upload() method in version 2.47. Before this you could use the value returned by param('upload_file') as the file handle in order to read from the file. I have had problems with the upload() method in versions of CGI.pm up to and including 2.75 so usually kludge around it and do it the old way if necessary.

    Anyway once you have your file handle you read from the temporary file and write it somewhere new so I don't understand what you mean in the second bit. A quick and dirty solution would just be to use Perl's rename($old, $new) function if everything works currently to get the final filename you want.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: problem changing name of uploaded file!
by marcus (Scribe) on Sep 03, 2001 at 20:20 UTC
    Hei Kenneth :)

    Hrm, if you want to improve your chances of getting help, you might want to try to learn how to post readable code here.

    Note that you can use html tags like paragraph and code to improve the readability of your code. Right now, trying to read your post gives me a headache :) Check out Writeup Formatting tips

    Good luck on finding the answer to your question, and note that if you had become a member, you would have been able to edit your original node, rather than writing a new one. It's not like it'll cost you anything =)

    qw[marcus</code>]</code>

Re: problem changing name of uploaded file!
by Anonymous Monk on Sep 03, 2001 at 20:03 UTC
    ouch..the post looks kinda weird...no spaces...heh. also, i forgot to say. im using cgi.pm 2.56 and perl 5.6.0 kenneth

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://109882]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2020-06-05 01:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (35 votes). Check out past polls.

    Notices?