in reply to Re: Files are corrupted after uploading
in thread Files are corrupted after uploading
#!/perl/bin/perl use CGI; use strict; use Win32::ODBC; use File::Basename; use CGI qw(:standard); use Fcntl qw(:DEFAULT :flock); use CGI::Carp qw(fatalsToBrowser); #Files upload info use constant UPLOAD_DIR => "files_uploaded/"; # Directory to + store the uploaded files use constant BUFFER_SIZE => 16_384; # Amount of uplo +ad file to read at one time use constant MAX_OPEN_TRIES => 100; $CGI::DISABLE_UPLOADS = 0; # Temporarily reenable upload +s $CGI::POST_MAX = -1; # This will stop someone from u +ploading my $grab_file = CGI->new; my $donepage = "upload_complete.pl"; ##################### # DECLARE VARIABLES # ##################### my (@message); # add all function messages to t +his bozo for easy debugging my (@statement); # an array of statements we may se +nd to database my (@connection); # array of database connections my ($record_id)=sprintf "%lx",time; # Randomly assign a record_id ###################### # CGI Parameters # ###################### my ($industry)= param("industry"); my ($topic)= param("topic"); my ($sources_used)= clean_sql(param('sources_used')); my ($relevant_information)= clean_sql(param('relevant_information') +); my ($researcher)= param("researcher"); my ($request_date)= clean_sql(param("request_date")); ###################################### # Establish main database Connection # ###################################### $connection[0]=new Win32::ODBC("DSN=mkrdb;"); unless ($connection[0]) { push @message,"Unable to connect to database!"; } push @message,$connection[0]->Error; # Stores the urls into the database STORE_URL: { for my $url_num (1..5) { my $url = param("url$url_num") or next STORE_URL; if ($url) { push @statement,"INSERT INTO websites (record_id, website) + VALUES ('$record_id', '$url')"; } } } # Uploads the files into the database UPLOAD_FILE: { for my $file_num (1..5) { my $file = $grab_file->param("file$file_num") or next UPLOAD_F +ILE; my ($base,$path,$type)=@_; my $file_handle = $grab_file->upload("file$file_num"); my $buffer = ""; if ($file) { $file =~ s/</</g; $file =~ s/>/>/g; fileparse_set_fstype("MSWin32"); ($base,$path,$type) = fileparse($file,'\..*'); my $type = lc $type; my $uploadedfile = $base . $type; $uploadedfile =~ s/[^\w.-]/_/g; if ( $uploadedfile =~ /^(\w[\w.-]*)/ ) { $uploadedfile = $1; } else { error( $grab_file, "Invalid file name; files must star +t with a letter or number." ); } # This will create the new file in the new location until ( sysopen OUTFILE, UPLOAD_DIR . $uploadedfile, O_RDW +R|O_CREAT|O_EXCL, 0777 ) { $uploadedfile =~ s/(\d*)(\.\w+)$/($1||0) + 1 . $2/e; $1 >= MAX_OPEN_TRIES and error( $grab_file, "Unable to + save your file. File 1" ); } # The file needs to be set to binmode in order to save it +on a Win32 system binmode OUTFILE; binmode $file_handle; # This will write the info into the new file while ( read($file_handle,$buffer,BUFFER_SIZE) ) { print OUTFILE $buffer; } close OUTFILE; # This will store the file name in the database push @statement,"INSERT INTO documents (record_id, documen +t) VALUES ('$record_id', '$uploadedfile')"; } } } # Insert the rest of the information into the database push @statement,"INSERT INTO research_info (record_id, industry, topic +, sources_used, relevant_information, researcher, request_date) VALUE +S ('$record_id', '$industry', '$topic', '$sources_used', '$relevant_i +nformation', '$researcher', '$request_date')"; ################### # Clean Sql stuff # ################### sub clean_sql { my $sql=shift; ### get rid of ms word junk $sql=~s/· |Þ |§ |\r|\*\t//g; $sql=~s/\n+$//g; $sql=~s/^\n+//g; ### replace dangerous SQL and ODBC characters with ASCI code $sql=~s/\'/\' \& chr\(39\) \& \'/g; $sql=~s/\"/\' \& chr\(34\) \& \'/g; $sql=~s/\|/\' \& chr\(124\) \& \'/g; $sql=~s/\:/\' \& chr\(58\) \& \'/g; $sql=~s/\//\' \& chr\(47\) \& \'/g; $sql=~s/\./\' \& chr\(46\) \& \'/g; return $sql; } # Execute sql statments and if there is an error report it foreach (@statement){ $connection[0]->Sql($_); if ($connection[0]->Error()){ print "<p><b>ERROR: <p>The SQL statement:</b><br> $_ <p> <b>ca +used the error:</b><br> ",$connection[0]->Error(); print "<p>Info:<p>"; print "<br>$_=",join ', ', param($_); die(); } push @message,$connection[0]->Error(); } # Close the conections foreach (@connection) { $_->Close(); } ######################################## # Redirect the user to the search page # ######################################## print $grab_file->redirect($donepage);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Files are corrupted after uploading
by Chady (Priest) on Jul 25, 2001 at 23:40 UTC |