Please visit our sponsor
UNKNOWN =************************************** = Name: CGI Perl - File Upload = Description:Shows you the key of how to upload a binary file using perl/cgi. (what nobody tells you (i had to figure it out on my own!)) PS if this code helps, i'd appreciate a vote = By: Dax Ahweng = = = Inputs:You will need to make a form with the following properties: <form enctype="multipart/form-data" method="post" action="/"> <input type="file" name="filex"> and of course, a submit button = = Returns:Me script returns a confirmation of the upload and size of the uploaded file. = =Assumes:You need to have a basic knowledge of Perl, including variables, arrays, splice(), binmode() and handles.. but not too important. All you need is logic. = =Side Effects:Not optimised for text file upload, assumes you are uploading a JPEG, allows only 1 file upload (but can be easily optimised for more) =This code is copyrighted and has limited warranties. =Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.262/lngWId.6/qx/vb/scripts/ShowCode.htm =for details. =************************************** #!/usr/bin/perl ### NOTE: this is to demonstrate binary file upload, text file uploading is simple, but if you want to know how to do textfile uploading, just remove all the binmode() functions and that's it! use CGI; ### Set Maximum Upload size to 2MB (prevent ppl from uploading extremely large files or other such malicious stuff) $CGI::POST_MAX = 2048; print "content-type: text/html\n\n"; ### Set Standard Input to Binary Mode (this is critical for binary data (e.g. images) - the key piece of this code which nobody tells you (except me)) binmode(STDIN); @inx = <stdin>; ### This is just for a single file upload.. you can improve the code to include boundary separations and stuff like that.. ### These two lines delete the boundary and file info which comes with the formfeed and leaves only the binary data splice(@inx,0,4); splice(@inx,$#inx,1); $in = join("",@inx); $in = substr($in,0,length($in) - 2);; ### This next sniplet assumes you are uploading a JPEG file and saves it as z.jpg in the folder this script is stored ### You can also further improve the script by retreiving the filename from the 'spliced' lines open(ff,"&gt;z.jpg"); ### Set file output to binary mode (since you're assuming to write a binary file) binmode(ff); print ff $in; close(ff); print "DONE, Uploaded Size: ".length($in)." bytes";