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

Good Monks,

I have the following code which is supposed to upload a picture file. Instead it creates an empty file in the expected place. I have looked it upside down and inside out and cannot find out what I hav done wrong. Perhaps a friendly monk could enlighten me?

if ($photo) { $photoname = join ("", 'ch',$count,'.jpg'); open (OUTFILE,"> $photodir$photoname") or die "Cant open photofile +: $!"; while (read($photo,$buffer,1024)) { print OUTFILE $buffer; } close(OUTFILE); }

Replies are listed 'Best First'.
Re: uploading jpg files to server
by davorg (Chancellor) on Nov 16, 2004 at 15:15 UTC

    You probably need to show us the bit before that. The bit where you set $photo.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thanks Dave
      #!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI ':standard'; my $chdata = "/home/chdata.txt", my $chtmpl = "/home/charters1.html"; my $chpage = "/home/charters.html"; my $photodir = "/home/images/ch/"; my $yctname = param('yname'); my $length = param ('length'); my $speed = param ('speed'); my $acc = param ('acc'); my $anc = param ('anc'); my $boats = param ('bts'); my $text = param ('text'); my $text1 = param ('text1'); my $photo = param ('photo'); my $photo1 = param ('photo1'); my $photo2 = param ('photo2'); my $photoname; my $photoname1; my $photoname2; my $buffer; if (-e "$chdata"){ open FH, "< $chdata" or die $!; flock (FH, 1) or die "Can't lock data for reading"; } my @count = <FH>; close FH; my $count = @count; print "Content-type: text/html\n\n"; if ($photo) { $photoname = join ("", 'ch',$count,'.jpg'); open (OUTFILE,"> $photodir$photoname") or die "Cant open photofile +: $!"; while (read($photo,$buffer,1024)) { print OUTFILE $buffer; } close(OUTFILE); } else { print "no file loaded"; exit; }
      That's the whole script to the upload :)
        i do it like this:
        use Fcntl qw( :DEFAULT :flock ); use CGI; use constant BUFFER_SIZE => 16_384; use constant MAX_FILE_SIZE => 1_048_576; # Limit each upload to + 1 MB use constant MAX_DIR_SIZE => 100 * 1_048_576; # Limit total uploads +to 100 MB use constant MAX_OPEN_TRIES => 100; no strict; $CGI::DISABLE_UPLOADS = 0; $CGI::POST_MAX = MAX_FILE_SIZE; our $page = new CGI; print $page->header(); print $page->start_html( -title => $TITLE, -style => { 'src'=> $style }, ); $page->cgi_error and error( $page, "Error transferring file: " . $page +->cgi_error ); my $filename = $page->param( "file" ) || error( $page, "No filename e +ntered." ); my $fh = $page->upload('file'); # upload takes the name of "inp +ut file upload"-field if (!$fh && $page->cgi_error) { print $page->header(-status=>$page->cgi_error); exit 0; } my $buffer; if ( dir_size( $UPLOAD_DIR ) + $ENV{CONTENT_LENGTH} > MAX_DIR_SIZE ) { error( $page, "Upload directory is full." ); } # Allow letters, digits, periods, underscores, dashes # Convert anything else to an underscore $filename =~ s/[^\w.-]/_/g; if ( $filename =~ /^(\w[\w.-]*)/ ) { $filename = $1; } else { error( $page, "Invalid file name; files must start with a lett +er or number." ); } # Open output file, making sure the name is unique until ( sysopen(OUTPUT, $UPLOAD_DIR . $filename, O_WRONLY | O_EXCL | O +_CREAT) ) { $filename =~ s/(\d*)(\.*\w+)$/($1||0) + 1 . $2/e; error( $page, "Unable to save your file." ) if $1 >= MAX_OPEN_ +TRIES ; } # This is necessary for non-Unix systems; does nothing on Unix binmode $fh; binmode OUTPUT; while ( read( $fh, $buffer, BUFFER_SIZE ) ) { print OUTPUT $buffer; } close OUTPUT; my $sql_insert = "INSERT INTO bonsai_pics (name,bonsai_id,filename,tim +estamp) VALUES (?,?,?,?)"; $dbh->do($sql_insert,undef, $page->param('name'), $page->param('bo +nsai_id'), $UPLOAD_DIR . $filename, $date) || error $page,"Could not +insert $sql_insert :\n"; print "<b>Added $filename</b><br />";
        if I remember it correctly: it's based on an example i once found...
        hope this helps (don't forget the binmode thingie ;-) )
        --
        to ask a question is a moment of shame
        to remain ignorant is a lifelong shame