Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re:'s upload gives local filename, not file itself

by wog (Curate)
on Feb 03, 2002 at 04:35 UTC ( #143019=note: print w/replies, xml ) Need Help??

in reply to's upload gives local filename, not file itself

The upload() function's return is meant to be used as a filehandle (a re-writing of your code):

#!/usr/bin/perl -w use strict; use CGI qw(:standard); # (update: in your code you didn't import anything?! # I guess that you didn't show us the real code then... # (copy-and-paste if your friend.) (I'm referring # to the 'use CGI' line, of course.)) my $fh = upload('file'); open TEMP, ">temp.jpg" or die "error opening temp.jpg for writing: $!\ +n"; binmode TEMP; # very important on Windows-based machines { # this block is so the assignment to $/ # will not effect the rest of the script local $/ = \1024; # tell <...> to read in 1024-byte chunks. print TEMP $_ while <$fh>; } close TEMP or warn "error closing temp.jpg: $!\n";

(The use of strict and warnings is strongly reccommended.)

Replies are listed 'Best First'.
Re: Re:'s upload gives local filename, not file itself
by Wallamaster (Novice) on Feb 03, 2002 at 04:56 UTC
    indeed that's not the original code, that was the code I simplified it down to thinking I had made some stupid mistake. your additions seemed to do the trick. thanks!

      ++wog for getting you on the right track and ++tilly for explaining where and why the problem was arising. I just thought that I would add this as an extra. To help show what you can do with the "local" filename and why you might want it. Also, if you are using, you might aswell use it.
      #!/usr/bin/perl use CGI; $co = new CGI; if (!$co->param()) { print $co->header, $co->start_html('upload a file'), "<table border=1><tr><td>", $co->center('upload a file'), $co->start_multipart_form, $co->filefield(-name=>'file1', -size=>30), $co->br, $co->submit(-value=>'Upload'), $co->end_form, "</td></tr></table>"; } else { $file = $co->param('file1'); print $co->header, $co->start_html('file uploaded'), "Uploading $file ...<br>"; @filename = split(/\\/, $file); open (FILE, ">safe/$filename[$#filename]"), print "... $filename[$#filename] uploaded."; print FILE <$file>; close FILE; } print $co->end_html;
      This is working, but of course, this script is JUST AN EXAMPLE, and you should not trust input forms. Use -T and check the file path for bad chars. Basic error checking, referrer checking, etc.
      "Better secure than sorry." -xtype

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://143019]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2021-06-23 18:51 GMT
Find Nodes?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)

    Results (121 votes). Check out past polls.