Hi, I have a script that's supposed to take 5 or less files and upload them to a specified directory. The program seems to work but once i check the files i get errors or the file just won't load. Here is the script:
#!/perl/bin/perl use strict; use CGI; use File::Basename; use Fcntl qw( :DEFAULT :flock); use CGI::Carp qw(fatalsToBrowser); use constant UPLOAD_DIR => "files_uploaded/"; use constant BUFFER_SIZE => 16_384; # Amount of upload file to + read at one time use constant MAX_OPEN_TRIES => 100; $CGI::DISABLE_UPLOADS = 0; $CGI::POST_MAX = -1; my $grab_files = CGI->new; my $donepage = "upload_complete.pl"; UPLOAD_FILE: { for my $file_num (1..5) { my $file = $grab_files->param("file$file_num") or next UPLOAD_ +FILE; my $filename = $grab_files->param("file$file_num"); my ($base,$path,$type)=@_; my $file_handle = $grab_files->upload("file$file_num"); my $format = $grab_files->uploadInfo($file)->{'Content-Type'}; my $buffer = ""; if ($file) { $filename =~ s/</&lt;/g; $filename =~ s/>/&gt;/g; fileparse_set_fstype("MSWin32"); ($base,$path,$type) = fileparse($filename,'\..*'); my $type = lc $type; my $uploadedfile = $base . $type; $uploadedfile =~ s/[^\w.-]/_/g; if ( $uploadedfile =~ /^(\w[\w.-]*)/ ) { $uploadedfile = $1; } else { error( $grab_files, "Invalid file name; files must star +t with a letter or number." ); } # This will create the new file until ( sysopen OUTFILE, UPLOAD_DIR . $uploadedfile, O_RDWR +|O_CREAT|O_EXCL ) { $uploadedfile =~ s/(\d*)(\.\w+)$/($1||0) + 1 . $2/e; $1 >= MAX_OPEN_TRIES and error( $grab_files, "Unable to + save your file. File 1" ); } while ( read($file_handle,$buffer,BUFFER_SIZE) ) { print OUTFILE $buffer; } close OUTFILE; } } } # Send them to the confirmation page. print $grab_files->redirect($donepage);
Someone please help. Thanks, Kiko.

In reply to Files are corrupted after uploading by Kiko

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.