Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: File source from memory

by maard (Pilgrim)
on Feb 18, 2005 at 19:07 UTC ( [id://432474]=note: print w/replies, xml ) Need Help??


in reply to File source from memory

First, beware that different browsers send file name differently, if you expect to use that file name. IE sends full file path while Opera sends just the basename of file. And if you want your file saved on host use something like

my $file_name = CGI::param('file_field'); $file_name =~ s:^.*[\\/](.*)$:$1:; # remove path if present my $fh = CGI::upload('file_field'); if ( $fh ) { open(OUT, '>', "/dir/to/save/$file_name"); while (<$fh>) { print OUT $_; } close OUT; } else { # handle the error }

If you don't need the file name you may don't bother about it's correctness and just read from filehandle (which is $fh - the value that CGI::upload gives you) and send file data to whatever destination you need.

Replies are listed 'Best First'.
Re^2: File source from memory
by Anonymous Monk on Feb 18, 2005 at 19:16 UTC
    Thanks for your help, I do know I have to strip the file name but I do NOT want to save the file to my server. I want to read the data from the file directly and write it to the database. How can I do this without first saving the file?

      Notice the scalar reference when using this technique.
      #!/usr/bin/perl #Do you want to use a perl variable as if it were a file? #Previous versions of perl had IO::Stringy and IO::Scalar for that. #Perl 5.8 with PerlIO has it natively. Just use a reference to #the scalar in the filename slot of open. my $foo = ''; open FILEHANDLE, '+>', \$foo or die $!; my $count = 0; for(1..100){ print FILEHANDLE $count; # print "$foo\n"; # select(undef,undef,undef,.1); $count++; } close FILEHANDLE or die $!; print "###########################\n"; print "$foo\n";

      I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found