473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Expanding a file upload script to handle 10 files instead of 1?

Hey there.

I've been trying to modify my file upload script so that it handles 10 files instead of one.

i was thinking the most straightforward way would be to add a FOR LOOP? placed strategically somewhere like just before the my variables get declared???

the POST input name is "fileup" so maybe i could call them fileup1, fileup2 etc.

This is the upld.pl script itself.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use CGI;
  4. use CGI::Carp "fatalsToBrowser";
  5. use strict;
  6. use DBI;
  7. use Data::Dumper;
  8. use Digest::MD5  qw(md5 md5_hex md5_base64);
  9.  
  10. require 'dbconfig.pl';
  11. require 'functions.pl';
  12. require 'server.pl';
  13. my %server = &getServer();
  14. my %config = &getDbConfig();
  15.  
  16. # Dump Post Data To File
  17. my $post_length;
  18. my $tmpfiledir = 'temp/';
  19. my $filedir = 'files/';
  20. my $query;
  21. my $tmpfilename;
  22. my $filename;
  23. my $line;
  24. my $f;
  25. my $readline;
  26. my $seperator;
  27. my $ender;
  28. my $fread;
  29. my $key;
  30. my $value;
  31. my $lenfilename;
  32. my %post;
  33. my $cookie;
  34. my $session;
  35. my $result;
  36. my $unique;
  37. my $session_expire;
  38. $post_length = $ENV{'CONTENT_LENGTH'};
  39.  
  40. binmode STDIN;
  41.  
  42. my $dbh;
  43. $dbh = DBI->connect('dbi:mysql:'.$config{'db_database'}.':'.$config{'db_server'},$config{'db_user'},$config{'db_password'})
  44.   or die ($dbh::errstr);
  45.  
  46. my %config = &getConfig($dbh);
  47.  
  48. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime(time);
  49. $year += 1900;
  50. $mon++;
  51. $filedir .= sprintf('%02d%02d%02d',$year,$mon,$mday);
  52. mkdir $filedir;
  53. $query = $ENV{'QUERY_STRING'};
  54. if($query =~ /unique=([a-f0-9]{32})/){
  55.   $tmpfilename = $tmpfiledir . $1;
  56.   $unique = $1;
  57.   $filename = $filedir  . "/$unique";
  58. } else {
  59.   # Some error message here
  60.   print "Content-type: text/html\n\n";
  61.   print "Error";
  62.   exit;
  63. }
  64.  
  65. $lenfilename = $tmpfilename . '.size';
  66. open TEMPFILE, ">$lenfilename";
  67. print TEMPFILE $post_length;
  68. close TEMPFILE;
  69.  
  70. open TEMPFILE, ">$tmpfilename";
  71. binmode TEMPFILE;
  72. while (read STDIN, $f, 4096 && $post_length > 0){
  73.   print TEMPFILE $f;
  74.   $post_length -= length $f;
  75. }
  76. close TEMPFILE;
  77.  
  78. open TEMPFILE, "<$tmpfilename";
  79. binmode TEMPFILE;
  80. $seperator = <TEMPFILE>;
  81. $seperator =~ /(.+?)(\r?\n)/;
  82. $ender = "$1--$2";
  83. my $fsize = 0;
  84. while ($readline = <TEMPFILE>){
  85.  
  86.   if ($readline =~ /^Content-Disposition: form-data; name="fileup"; filename="(.+?)"/)
  87.   {
  88.     $post{'filename'} = $1;
  89.     open DFILE, ">$filename";
  90.     binmode DFILE;
  91.     $fread = <TEMPFILE>;
  92.     $post{'contenttype'} = '';
  93.     if($fread =~ /^Content-Type: ([a-zA-Z0-9\/-]+)/){
  94.       $post{'contenttype'} = $1;
  95.     }
  96.     $fread = <TEMPFILE>;
  97.     while (($fread = <TEMPFILE>) && ($fread ne $seperator) && ($fread ne $ender)){
  98.         $fsize += length $fread;
  99.       print DFILE $fread;
  100.     }
  101.     close DFILE;
  102.   }
  103.   else{
  104.   if ($readline =~ /^Content-Disposition: form-data; name="(.*?)"/){
  105.     $key = $1;
  106.     $fread = <TEMPFILE>;
  107.     $value = '';
  108.     while (($fread = <TEMPFILE>) && ($fread ne $seperator) && ($fread ne $ender)){
  109.       $value .= $fread;
  110.     }
  111.     $value =~ s/^(.*)\r\n$/$1/;
  112.     $post{$key} = $value;
  113.   }
  114.   }  
  115. }
  116.  
  117. if($fsize > $config{'upload_max_size'}){
  118.   print "Content-type: text/html\n\n";
  119.   print "<html><head><title>File Uploaded</title></head><body onload=\"parent.location.href='".$config{'site_basedir'}."/filetoobig/'\">";
  120.   print "File Too Big";
  121.   print "</body></html>";
  122.   die();
  123. }
  124.  
  125. my $extension = '';
  126. if($post{filename} =~ /\.([^\.]+)$/){
  127.   $extension = $1;
  128. }
  129.  
  130. if ($config{upload_blocked_extensions} =~ /\b$extension\b/){
  131.   $post{filename} .= '.renamethis';
  132. }
  133.  
  134. close TEMPFILE;
  135.  
  136. $session_expire = $config{'user_session_expire'};
  137.  
  138. print "Content-type: text/html\n\n";
  139.  
  140. $cookie = $ENV{'HTTP_COOKIE'};
  141. if($cookie =~ /session=([a-f0-9]{32})/){ 
  142.   $session = $dbh->quote($1);
  143. } else {
  144.   $session = "''";
  145. }
  146. my $userip = $ENV{REMOTE_ADDR};
  147.  
  148. $query = "SELECT `session_user_index` FROM `sessions` WHERE `session_unique`= $session AND `session_time`>(UNIX_TIMESTAMP() - $session_expire) LIMIT 0,1;";
  149.  
  150. $result = $dbh->prepare($query);
  151. $result->execute()
  152.   or die $result::errst;
  153.  
  154. my $userindex;
  155. $userindex = $result->fetchrow();
  156. if ($userindex){
  157.   #user is logged in
  158. } else {
  159.   # user is not logged in
  160.   $userindex = -1;
  161. }
  162.  
  163. if($post{'filename'} =~ /\/([^\/])$/){
  164.     $post{'filename'} = $1;
  165. }
  166.  
  167. $query = "INSERT INTO `files` (`file_server_index`,`file_unique`,`file_disk_location`,`file_name`,`file_mime`,`file_size`,`file_user_index`,`file_description`,`file_upload_ip`,`file_upload_time`,`file_hits`,`file_downloads`,file_last_download_time) VALUES (".
  168.   $dbh->quote($server{'server_index'}).','.
  169.   $dbh->quote($unique).','.
  170.   $dbh->quote($filename).','.
  171.   $dbh->quote($post{'filename'}).','.
  172.   $dbh->quote($post{'contenttype'}).','.
  173.   $dbh->quote($fsize).','.
  174.   $dbh->quote($userindex).','.
  175.   $dbh->quote($post{'description'}).','.
  176.   $dbh->quote($userip).','.
  177.   $dbh->quote(time).','.
  178.   '0,0,UNIX_TIMESTAMP()'.  
  179.   ");";
  180.  
  181. $result = $dbh->prepare($query);
  182. #print $query;
  183. $result->execute
  184.   or die $result::errstr;
  185.  
  186. print "<html><head><title>File Uploaded</title></head><body onload=\"parent.location.href='".$config{'site_basedir'}."/fileuploaded/$unique'\">";
  187. print "File uploaded sucessfully";
  188. print "</body></html>";
  189.  
  190. #################################
Anyone know if i can simply wrap it in a for loop? and if so where & would references would i need to change? I assume only the "fileup" texts?
Dec 10 '06 #1
1 1815
GunnarH
83
I suggest that you use the CGI::UploadEasy module. Check out the example script for a starting point.
Dec 10 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
12
by: JMB | last post by:
Hello, I was wondering if anyone knew of any projects extending the inline upload progress bar to utilize an inpage image uploader with bar, without having to refresh or go to a seperate page,...
7
by: ljuljacka | last post by:
I'm just trying to run a fileupload script from the manual, just to see how it works, and it won't. I've checked if file upload is enabled and it is. Also, the file I'm trying to upload is smaller...
6
by: Vic Spainhower | last post by:
Hello, I am trying to do a FTP file upload which works fine on my localhost but on my ISP server it fails. I can't seem to find where I can go to find the specific cause of the failure. In both...
1
by: pbd22 | last post by:
hi. i have been posting this here and elsewhere a lot and can't seem to get resolution on this problem. i have been trying to upload files using a hidden iframe to a asp.net/vb.net form. the...
3
by: dreamznatcher | last post by:
Hello, I found a script here: http://www.webtoolkit.info/ajax-file-upload.html which supposedly allows you to upload files using AJAX (I'm not an expert). The site claims it's the best way to...
3
by: pozze | last post by:
Hi, I've just made the change from ASP to .net. I have a file (code below) that saves a user submitted file to a MS SQL 2005 database. It collects the file name, file size, file type, and lastly...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.