Question : Need help in creating a short script to upload a file to my server.

I hope someone can help me.

What I am trying to do, but can't seem to get it done is as follows.

I need help in creating a Perl script and accompanying HTML page, that will allow a user to browse on their computer to find a file (a minimum of one, but possibly up to 10 different files) and once they found the file, it would then allow the user to submit the file to automatically FTP to our server (hosted by an outside company like VERIO).

I am thinking it would be easier if they could upload all possible files in one shot, with individual checking (as noted below) for each file.

If the file already exists on the server (in the particular directory that it is being sent to) the script should ask the user if they want to overwrite the existing file.

If yes, it does. (And returns them back to the original HTML page.) If not, it will also return them back to the original HTML page to search for a new page.

If someone can help me by creating the Perl script (and HTML page) to do this ... I would very much appreciate it.

The type of files that I would like to allow (at this time) for upload will be with the following extensions.   (.doc, .rtf, .pdf., and maybe a few others.)  But, I would also like an option of not restricting the script to certain file types.  Let me know what I can do about this as well.

And, I would like to receive an email that lets me know when a file was uploaded (including the name of the file, the destination of where it was uploaded to, the size of the file and the date and time it was uploaded).

Please let me know if this is possible.

Thanks,
Gary

Answer : Need help in creating a short script to upload a file to my server.

garymgordon,

"..Thanks for the explanation...."

You're welcome.

Here is the latest version of the code.

Let me know how it goes.

===multi_upload.pl
#!/usr/local/bin/perl

use CGI;
$query=new CGI; ##  Create a new CGI object

##  Print the MIME header.
print "Content-type: text/html\n\n";

##  Full path to the sendmail program
$mailprog ="/usr/lib/sendmail";

$to='[email protected]';
$from='[email protected]';

##  Full path to the directory where you want to store the uploaded files.
$dir_to_store="/tmp";
##  Counter to count the no of files that were uploaded
$no_of_files=0;
@list=();

##  Go through the HTML form values.
foreach ($query->param){
  ##  Check how many files have been uploaded
 ##  We look at HTML form variables which start with doc ...
  ##  ..followed with a number.
  if (/^doc\d+$/ && $query->param($_)){
    ++$no_of_files;
    push(@list,$_); ##  Store the names of these files in an array.
  }
}

if (! $no_of_files){  ##  No file selected for upload
  print "Enter at least one file for upload.
\n";
  exit;
}else{  ##  File/Files has/have been selected for upload.
  foreach (@list){
    ##  Get the name of the uploaded file
    ##  Store it in 2 variables.
    $uploaded_file=$query->param($_);
    $tmp_uploaded_file=$query->param($_);

    ##  Convert all \'s to /'s
    $tmp_uploaded_file=~ s/\\/\//g;
    @tmp_uploaded_file=split(/\//,$tmp_uploaded_file);

    ##  Get the name of the file only.
    $filename = $tmp_uploaded_file[$#tmp_uploaded_file];

    ##  Allow upload of ONLY those files that have the
    ##  ... valid extensions
    if (!(
    $uploaded_file =~ /\.pdf$/i ||
    $uploaded_file =~ /\.doc$/i ||
    $uploaded_file =~ /\.rtf$/i
    )){
      print "Invalid file type, Skipping $uploaded_file!!>\n";
      push(@message,"Invalid file type, Skipped $uploaded_file"); ##  Store the message.
      next; ##  Skip this file and go to the next one.
    }

    ##  Create the full path to the file on the server.
    $new_file=$dir_to_store."/".$filename;

    open(MYFILE,"> $new_file") || die $!;
  binmode MYFILE;   ##  Open the file in binary mode.
    while($bytesread=read($uploaded_file,$data,1024)){
      ##  Calculate the size of the file.
      $size+=$bytesread;
      ##  Also add to the contents of the output file.
      print MYFILE $data;
    }
    close(MYFILE);
    close($uploaded_file);

    ##  Store the messages for emailing later.
    push(@message,"$uploaded_file uploaded ($size bytes)");

    ##  Print Some message to the browser.
    print "$uploaded_file has been uploaded.
\n";
    print "Size = ",$size," byte(s)

\n";
    $size=0;  ##  Re initalize the size variable.
  }

  $now=`date`;
  chomp($now);  ##  Remove the newline character.

  ##  Open sendmail, exit with cause of the error if open fails.
  open(MAIL, "| $mailprog -t ") || die $!;
  print MAIL "To: $to\n";
  print MAIL "From: $from\n";
  print MAIL "Subject: File upload report ($now)\n";
  print MAIL "Upload done from $ENV{'REMOTE_ADDR'} at $now\n";
  print MAIL "=============================================\n\n";

  ##  Print all the messages collected so far.
  print MAIL join("\n",@message);

  close(MAIL);
}

Random Solutions  
 
programming4us programming4us