Quick Search for:  in language:    
Shows,upload,binary,file,using,perlcgi,nobody
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 74,273. lines
 Jobs: 25. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for Perl.
Click here to see a screenshot of this code!Mailing List v2.0
By Aaron L. Anderson on 1/7

(Screen Shot)

ShowIMG
By Jeff Mills on 1/5


Simple Perl Ping
By John Hass on 12/29


Very basic login script template with cookies
By Aaron L. Anderson on 12/29


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

CGI Perl - File Upload

Print
Email
 
VB icon
Submitted on: 12/21/2001 2:46:02 PM
By: Dax Ahweng  
Level: Beginner
User Rating: By 15 Users
Compatibility:5.0 (all versions)

Users have accessed this code 21352 times.
 

(About the author)
 
     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
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    =**************************************
    = Name: CGI Perl - File Upload
    = Description:Shows you the key of how t
    =     o upload a binary file using perl/cgi. (
    =     what nobody tells you (i had to figure i
    =     t 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 wi
    =     th the following properties:
    <form enctype="multipart/form-data" method=post action="~script_file_location~">
    <input type=file name=filex>
    and of course, a submit button
    =
    = Returns:Me script returns a confirmati
    =     on of the upload and size of the uploade
    =     d file.
    =
    = Assumes:You need to have a basic knowl
    =     edge of Perl, including variables, array
    =     s, splice(), binmode() and handles.. but
    =     not too important.
    All you need is logic.
    =
    = Side Effects:Not optimised for text fi
    =     le upload, assumes you are uploading a J
    =     PEG, allows only 1 file upload (but can 
    =     be easily optimised for more)
    =
    =This code is copyrighted and has    = limited warranties.Please see http://w
    =     ww.Planet-Source-Code.com/vb/scripts/Sho
    =     wCode.asp?txtCodeId=262&lngWId;=6    =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,">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";

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
12/29/2001 11:24:31 PM:Kalpana
Hi Dave,
Even I am a beginner in 
Perl, and found your code simple and 
easy too.
I have written some good 
guestbooks, form emailers, and 
discussion forums in perl.
Kalpana
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/31/2001 4:51:09 PM:Dax Ahweng
yea, cool.. all you have to remember is 
the binmode, otherwise it outputs as a 
text file :)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/6/2002 7:17:27 PM:Matthias Spinner
Hmm, don't know why, but it seems like 
it doesn't work correctly when I try 
it.
I can upload a picture without 
problems, but when I want to look at 
it, it can't load it (like it wouldn't 
exist.) but with the ftp programm I can 
see it, and when I download it with it 
and start it on my local hard disc, I 
see it perfectly... what am I doing 
wrong?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
2/6/2002 11:37:57 PM:Dax Ahweng
hmm... well, i cant tell, since you 
said it works fine when you download it 
with the ftp program, the script must 
be fine.. maybe there's a problem with 
your URL? or something?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/15/2002 1:51:51 PM:meth0s
Nice man I am new to perl and a example 
like this exlpains everything to me
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
3/20/2002 6:51:41 PM:bruno
it's kool... 
this example exlpains 
some things very good
[de unde sugy 
pula]
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/6/2002 5:32:59 AM:Super--s
I wish there were more than just about 
30 cgi uploading scripts on the net...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/6/2002 4:16:23 PM:Dax Ahweng
If you need any help in modifying the 
above or creating your own upload 
script, i can give an overview/help if 
you want...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/23/2002 11:20:21 PM:ms
Hey your codes are really great man.. 
but i'm having a small problem here, 
and wondering whether you can help me 
with it.
I wrote this code to upload a 
file using 2 methods. One is using a 
textfile to type in the file name, then 
use a submit button. the other one is 
by entering the file name inside the 
code itself using a hidden object and a 
submit button. Somehow the one with the 
hidden object is not working. 
Would 
you know why it's not working??
Thanks 
once again man!!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/24/2002 7:40:48 AM:Dax Ahweng
i'll email you concerning this!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/27/2002 4:22:00 AM:Akujin
it's nice to know some people still put 
value into commenting their code. I'm 
new to perl, and it's a bit confusing 
because i work a lot with Object 
Oriented Programming such as Java and 
C++. Yours is the only file upload 
script i've found that is commented so 
i know what the hell is going on. Thanks
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/30/2002 5:46:24 PM:wwendorf
I used this code, and it works great, 
but I need to be able to use the 
cgi-lib library, and the same 
techniques you use in the cgi.pm don't 
work in cgi-lib.  I have gotten files 
to upload in cgi-lib, but only text 
files work.  I can't get binary files 
to upload without scrambling to the 
point they won't open.  Any idea?  Feel 
free to email me direct if you 
can.
Thanks,
Wade
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
1/21/2003 7:24:10 PM:
I am new to perl.  I am trying to use 
this script to move a graphics file 
from one of my servers and make it 
available on the other.  I have tried 
to modify the script to work from a 
file that I have opened instead of 
STDIN.  I just can't seem to get it to 
work.  Any ideas?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/3/2003 1:21:20 PM:
#catch the header 
stuff
($id,$data,$type) = 
splice(@inx,0,4);
splice(@inx,$#inx,1);
#split the 
data
($tmp,$button,$fileName) = 
split(/;/,$data);
#replace all with 
the name value
$button=~s/.*name=
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/3/2003 1:22:41 PM:
try again
($id,$data,$type) = 
splice(@inx,0,4);
splice(@inx,$#inx,1);
($tmp,$button,$fileName) = 
split(/;/,$data);
$button=~s/.*name="(.
*)".*/$1/;
$fileName=~s/.*filename="(.*
)".*/$1/;
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/24/2003 12:25:52 AM:
how can i save multiple files and not 
have it overwriting all the time? i am 
way too new at this to know anything 
:)
 could someone email me the 
answer?mike.opitz@shaw.ca
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
4/28/2003 4:12:19 AM:Dax Ahweng
well, i emailed you the answer.. didnt 
know you had wrote here as well until 
now.. erm, there is a guy that took my 
code and made it save to the same 
filename as the file being uploaded, so 
that could be useful!
vote for me 
people :D!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/23/2003 5:25:30 AM:
The jpg was uploaded but the picture is 
no longer readable??!!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/23/2003 11:48:06 AM:Dax Ahweng
I need more info to know what's wrong.. 
otherwise, i have no idea why it's not 
readable...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/27/2003 3:55:59 PM:
MAN THIS IS AWESOME! I'VE BEEN LOOKING 
FOR THIS SCRIPT FOREVER!  IF YOU COULD 
HAVE THIS SENT TO MY EMAIL USEING 
SENDMAIL THAT WOULD BE EVEN BETTER!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
6/28/2003 1:05:10 PM:Dax Ahweng
- filename -
you can extract the 
filename from the spliced lines at the 
beginning of @inx
and replace z.jpg 
with that!
- sendmail -
i know 
it's possible, i just dont know how 
it's done, the info for that is readily 
available though, on the 
internet...
search for qmail as the 
sendmail program...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/14/2003 12:08:15 AM:Chris Stratford
can you please email me a modified 
version, where you can input a filename 
to save as?
eg. if you want to 
upload a file, and you need to pick a 
filename. i want the form to 
have:
<BROWSE - the File 
Address>
and
<FiLENAME - what you 
want to save as!>
and then 
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/14/2003 12:08:40 AM:Chris Stratford
and then
<SUBMIT>
btu yeah
btw - 
my email is
neester_@hotmail.com
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/14/2003 12:11:08 AM:Chris Stratford
sorry its me again...
use CGI 
qw(:standard :form);
my $query = new 
CGI();
my $q_file = 
$query->param('filex');
my $q_filename 
= $query->param('filename');
can 
you use that sort of thing 
here?
because htat would be soooo easy 
then (wouldn't it?)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/24/2003 10:12:27 AM:
Great Script! I want to add it to my 
existing form, so how do I modify the 
script so instead of going through 
STDIN, it only goes for the filex 
variable.
Thanks in Advance!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/24/2003 12:24:34 PM:Dax Ahweng
well, STDIN is the method by which the 
data is passed to the script... the 
only other way i can think of is the 
$ENV{QUERY_STRING} if i remember 
correctly... and this way is impossible 
cause you cant pass binary data through 
the address bar, lol!
if i get some 
free time, which is quite probable, i 
will just improve on this code.
the 
idea was to give you the 'secret' to 
BINARY file uploads... an average 
programmer should be able to do the 
rest...
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/24/2003 12:28:31 PM:Dax Ahweng
oh.. but if you want to separate the 
data from the rest of the form without 
splicing it, you can simply process the 
boundaries (make a test script to 
analyse the boundaries standard format 
by print(ing) the whole formfeed... 
then devise code to filter the data 
(removing all the boundaries in a more 
systematic way than splicing them off, 
since the boundary header contains the 
data info, including filename!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
7/24/2003 12:45:16 PM:Dax Ahweng
ps, i'd appreciate a vote people ;)
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
12/10/2003 2:42:26 PM:
How can you load in a text file or a 
binary file, I assume do a check on 
STDIN to see what type of data it is, 
and then set your mode, but by that 
time, you have already read something 
from standard in... so how do you 
handle both a text and a binary file?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Perl Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.