upload,file,type,your,website,offer,unique,ab
Quick Search for:  in language:    
upload,file,type,your,website,offer,unique,ab
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Perl Stats

 Code: 55,644 lines
 Jobs: 120 postings

 
Sponsored by:

 

You are in:

 
Login



Latest Code Ticker for Perl
Anonymous Mailer (Rev. 2)
By Robert Wolfe on 8/24


E-Mail Bomber (flood)
By Robert Wolfe on 8/24


Click here to see a screenshot of this code!Damian's DataBase
By Damian myerscough on 8/22

(Screen Shot)

Adult Check Checker
By iron saTan on 8/22


Adult Check Catcher....
By iron saTan on 8/22


Adult Check Catcher
By iron saTan on 8/22


Click here to see a screenshot of this code!Anonymous E-Mail Send
By Robert Wolfe on 8/22

(Screen Shot)

Click here to see a screenshot of this code!Ping IP Address From Web Site
By Terrence Wright on 8/21

(Screen Shot)

Text Counter
By Bhushan Paranjpe on 8/21


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 File Upload (with filename acquisition)

Print
Email
 
VB icon
Submitted on: 8/1/2002 3:18:44 AM
By: Akujin  
Level: Beginner
User Rating: By 5 Users
Compatibility:5.0 (all versions), Active Perl specific, 4.0 (all versions), 3.0 (all versions), Pre 3.0

Users have accessed this code 491 times.
 
(About the author)
 
     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...)
 
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 File Upload (with filename a
    =     cquisition)
    = Description:To upload a file of any ty
    =     pe to your website. I offer you the uniq
    =     ue ability to call the original filename
    =     , and save the file as it was intended, 
    =     not under a temporary, or alternate, fil
    =     ename. (though... if you want to force t
    =     he name for some reason, it's easy enoug
    =     h to modify my code to do so...)
    = By: Akujin
    =
    = Inputs:The code comes with the nessica
    =     ry 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 g
    =     uess that counts as a return!
    =
    = Assumes:My code is VERY well commented
    =     and you should not have a problem with a
    =     nything, however if you DO then feel fre
    =     e to e-mail me, or comment! I'm happy to
    =     answer any questions that you may have.
    =
    = Side Effects:Nothing. This script is c
    =     ompletly safe.
    =
    =This code is copyrighted and has    = limited warranties.Please see http://w
    =     ww.Planet-Source-Code.com/xq/ASP/txtCode
    =     Id.348/lngWId.6/qx/vb/scripts/ShowCode.h
    =     tm    =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: #
    # <form enctype="multipart/form-data" method=post action="/cgi-bin/file1.pl"> #
    # <input type=file name=filex>#
    # <input type=submit value="Submit">#
    # </form> #
    # 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 = <STDIN>;
    @filetest = <STDIN>;
    ###################################################################
    # 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 "<br>View file at: <a href=\"http://akujindomains.com/pics/$filename2\">$filename2</a>";
    ####################################################################
    # 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 #
    ####################################################################

 
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

 There are no comments on this submission.
 
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.