Please visit our sponsor
UNKNOWN
=**************************************
= Name: CGI File Upload (with filename acquisition)
= Description:To upload a file of any type to your website. I offer you the unique ability to call the original filename, and save the file as it was intended, not under a temporary, or alternate, filename. (though... if you want to force the name for some reason, it's easy enough to modify my code to do so...)
= By: Nate Cox
=
=
= Inputs:The code comes with the nessicary HTML form required to use this script. nothing complex, and you can add it to ANY html webpage!
=
= Returns:Well, it uploads the file! i guess that counts as a return!
=
=Assumes:My code is VERY well commented and you should not have a problem with anything, however if you DO then feel free to e-mail me, or comment! I'm happy to answer any questions that you may have.
=
=Side Effects:Nothing. This script is completly safe.
=This code is copyrighted and has limited warranties.
=Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.348/lngWId.6/qx/vb/scripts/ShowCode.htm
=for details.
=**************************************
#!c:/perl/bin/perl.exe
###############################################################################
# before you start, you are going to need an HTML form to use this#
# script. Basically a way to call the script to be used. #
# i'll enclose the form you need below: #
# #
# just replace the "/cgi-bin/file1.pl" in the top with whatever you decide to #
# name the file.#
###############################################################################
########################
# enable CGI interface #
########################
use CGI;
###########################
# Set maximum upload size #
###########################
$CGI::POST_MAX = 2048;
#####################
# post content type #
#####################
print "content-type: text/html\n\n";
###################################################################
# Now we really begin. binmode() sets the argument passed into it #
# to a binary file. This means that it can be ANY type of file #
# because EVERYTHING is base binary. Without this line you will #
# only be allowed to successfully upload text files. The next #
# line sets the arguments passed from our form (which perl calls #
# STDIN) to a binary mode. #
###################################################################
binmode(STDIN);
###################################################################
# The next two lines are simple. We are passing the array "STDIN" #
# into two differant arrays. @inx will be used for opening and #
# saving the file, while @filetest will be used to pull the files #
# name out. We must seperate STDIN into both of these arrays #
# because @inx is going to be taken apart later, and we can't #
# risk losing anything!#
###################################################################
@inx = ;
@filetest = ;
###################################################################
# Finally we arive at one of the most frustrating parts of #
# Writing this script! Obviously we want to know the name of the #
# original file because we need to know what to save it as. #
# Perl does NOT make this easy for us. one of the elements of the #
# STDIN's arguments is a string that tells us EVERYTHING about #
# the file. the problem is that it gives too much information #
# with no way to easily get one aspect. To show you what i mean #
# i'll give you the argument below:#
# Content-Disposition: form-data; name="filex"; filename="C:\Docu #
# ments and Settings\Owner\Desktop\Dracula.txt" #
# As you can see, it tells us everything we would want to know, #
# however pulling that information out is very tricky. I came up #
# with a way. I start by reversing the entire string. This makes #
# the filename start from the beginning of the string. backwards. #
# Then I search through the string looking for a "\" (the#
# backwards equivilent of "/") because we know the filename will #
# start at the end of the directory settings. As i look for the #
# "\" I store each character in a string, and by the time i find #
# the "\" i have the filename (backwards). Then it's a simple #
# matter of re-reversing the string and we have our filename! #
# you will see below!#
###################################################################
$str = @inx[1];
$str1 = reverse $str;
$filename1 = "";
$currchar = "";
$offset = 3;
while ($currchar !~ /\\/) {
$currchar = substr($str1, $offset, 1);
if ($currchar !~ /\\/) {
$filename1 = "$filename1$currchar";
$offset++;
}
}
$filename2 = reverse $filename1;
####################################################################
# We now have the filename stored in "$filename2". Lets move on. #
# Now, as i said before, everything is stored in STDIN, so we know #
# that the actual file is there too. Finding it is fairly simple. #
# First of all we cut up (or "splice") the array containing STDIN #
# I must Thank Dax Ahweng for this part of my code. He took the #
# time to figure out where the actual file was stored. Without #
# the code from him i never would have known where to start! #
# Anyways, we now splice @inx, removing the unnessisary elements #
# from STDIN. Then, in the following lines we further remove #
# the unnessisary characters, and strip @inx down to EXACTLY the #
# file data we need, storing that data in "$in" #
####################################################################
splice(@inx,0,4);
splice(@inx,$#inx,1);
$in = join("",@inx);
$in = substr($in,0,length($in) - 2);;
####################################################################
# from here on out everything is VERY simple. First, we open the #
# file that we wish to save. ">" means we want to create a new #
# file and overwrite anything that is named the same. The#
# directory you will have to change. I wrote this on a windows #
# computer, running an Apache server. MAKE SURE YOU KEEP THE #
# "$filename2" AT THE END!!!!!!!!!! This is our filename! without #
# this line we are not saving the file as anything!!!#
# We then set the file to a binary file (to read our binary STDIN) #
# and print the data to the file (note: after print it says ff #
# which is our filehandle. it means we are printing to that file, #
# not the screen. Finally, we close the file. #
####################################################################
open(ff,">../htdocs/pics/$filename2");
binmode(ff);
print ff $in;
close(ff);
####################################################################
# below this you may do whatever you wish. I personally just have #
# it display the file size, and a link where you can access the #
# file. but if you wish this is where you could redirect the page #
# or do whatever you need to do for your website #
####################################################################
print "DONE, Uploaded Size: ".length($in)." bytes\n";
print "
View file at: $filename2";
####################################################################
# I hope i didn't go too overboard on the commenting for you guys #
# (and girls, or corse)! I HATE finding scripts that are so #
# under-commented that you don't know what they are doing!! #
# i tried to keep the comments clean and precice, however if you #
# have any trouble with them... or you just don't like them... #
# feel free to let me know, and suggest how i might change them! #
# Also, if you like the code (and i spent a lot of time and effort #
# writing this for you guys so that you could have the original #
# filename) i would REALLY appreciate a vote! Thanks a lot! #
# Any questions or comments can be directed to akujin11@yahoo.com #
####################################################################