Simple Upload
Below is a simple file upload script. It has a dual purpose, something easy to install for those who can use it and something to study for those who want to see how it's done.
Within the script itself, you specify:
-
Where the uploaded file is stored (may be blank to store in the same directory where the script is installed).
-
The file name (optional).
-
The URL of a "thank you" page (also optional).
Do all edits with an ASCII/plain text word processor like NotePad or TextWrangler.
Here is the script. Save it on your hard drive with a .cgi file name extension.
#!/usr/bin/perl ############## # # Simple Upload # Version 1.0 # Copyright 2007 Bontrager Connection, LLC # # License Agreement at https://www.willmaster.com/software/information/software-license-agreement.php # must be agreed to before installation or use of this software. # # # ############## # Leave next two lines as is. use strict; # To specify a strict use of variables and functions use CGI; # To handle much of the upload work. ################## # Customizations are below. # # This script will display an upload form if launched # without uploading a file. # The source code of the form thus displayed may be copied # and pasted into a web page of your creation. ######### ################## ## C U S T O M I Z A T I O N S ######### # # # Three customizations may be made. # # One: # # Specify the directory where the file shall be stored. # A path beginning with "/" means relative to document root # directory. Otherwise, it means relative to directory # where script is installed. # Blank means store in the same directory where this script # is installed. # NOTE: Directory must exist and have sufficient permissions. my $StoreFileDirectory = ''; # # # Two: # # Specify the file name the uploaded file is to be stored as. # Blank means store file with same name as uploaded file. # NOTE: File will overwrite any existing with same # name as file being stored. my $StoreFileName = ''; # # # # Three: # # Specify the URL of the "thank you" page to display after # form is used. # Blank will cause another upload file to be presented. my $ThankYouPage = ''; ######### ## E N D O F C U S T O M I Z A T I O N S ################## # Load the contents of the form into the $FORM variable. my $FORM = new CGI; sub StoreUploadedFile { # Grab the filehandle of the uploaded file. my $filehandle = $FORM->upload('filename'); # Return false if no file uploaded. return '' unless $filehandle; # Grab the file name of the uploaded file. my $filename = $FORM->param('filename'); # Strip any path information from the file name. $filename =~ s!^.*[/\\]!!; # Determine file storage name and directory location. $StoreFileName = $filename unless $StoreFileName =~ /\w/; $StoreFileDirectory .= '/' if $StoreFileDirectory =~ /\w/ and $StoreFileDirectory !~ m![\\/]$!; $StoreFileDirectory = "$ENV{DOCUMENT_ROOT}$StoreFileDirectory" if $StoreFileDirectory =~ m!^[\\/]!; # Store the file. my $buffer; open UPLOADED,">$StoreFileDirectory$StoreFileName"; binmode UPLOADED; # for Win/DOS operating systems while(read $filehandle,$buffer,1024) { print UPLOADED $buffer; } close UPLOADED; # Return true. return 1; } # sub StoreUploadedFile sub PresentUploadForm { # Notice the enctype attribute of the form. print "Content-type: text/html\n\n"; print <<FORM; <html><body> <form name="UploadForm" enctype="multipart/form-data" action="http://$ENV{SERVER_NAME}$ENV{REQUEST_URI}" method="POST"> <input type="file" name="filename" size="55"> <br> <input type="submit"> </form> </body></html> FORM } # sub PresentUploadForm my $stored = StoreUploadedFile; exit PresentUploadForm unless $stored and $ThankYouPage; print "Location: $ThankYouPage\n\n"; # end of script
To install the script:
-
Make any edits you wish to make.
-
Upload the script into a directory on your server that can run Perl CGI scripts. Upload with FTP as a plain text file, not as a binary file.
-
Give the script 755 permissions.
To use, type the URL of the script into your browser. The script will generate its own upload form.
If you wish to put an upload form on a web page you create, let the script generate the form, then copy the source code and paste it into your web page. Easy, huh?
More than one copy of the script may be installed, either by by naming each copy differently or installing in separate directories.
For web developers, this may be just the ticket for clients to use when they wish to update an image or other file on a regular basis without calling you every time.
Or, use it for yourself. It's probably the easiest upload process around.
If you're interested in studying how the upload is done, you'll find the script well commented.
The actual uploading and storing of the uploaded file is only about a dozen working lines.
Will Bontrager