Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryManaging Website Forms

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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:

  1. Where the uploaded file is stored (may be blank to store in the same directory where the script is installed).

  2. The file name (optional).

  3. 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:

  1. Make any edits you wish to make.

  2. 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.

  3. 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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC