Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

cgi script to upload a file and use it in a perl script

cgi script to upload a file and use it in a perl script

cgi script to upload a file and use it in a perl script

I Have no experience in writting CGI scripts,so i am kind of stuck even how to start,if anybody can guide me how to go about it ,it would be great.
I am trying to upload a file through a cgi script and then pass the file to the perl script.The passing of the contents of the file are totally confusing me.
the user should be able to go to a url and specify his file through a form.

If anyone can give me a direction as to how it is done,i would appreciate it.


RE: cgi script to upload a file and use it in a perl script

a friendly note that there is a CGI forum that this thread might belong in..... ;^)

there is a link on the front page of www.perl.com that reads 'How can I learn to write CGI scripts'.  That is a good place to start.  Additionally,  try www.cgi101.com. ; They also have a decent 'how-to'.  

There have been a few threads on this very topic.....
see Thread219-28183 posted a piece of code in that thread which creates the upload form and then uploads the file.

'hope this helps.

 keep the rudder amid ship and beware the odd typo

RE: cgi script to upload a file and use it in a perl script

<FORM METHOD="POST" ACTION="script.pl" ENCTYPE="multipart/form-data">
<INPUT TYPE="FILE" SIZE="30" NAME="UPLOAD" ACCEPT="text/plain,image/gif">

You have to use the multipart/form-data enctype on the form.

I'm not sure if that ACCEPT thing works since I haven't had too much luck with it myself, but I saw it in the docs.  It's supposed to prevent the uploading of unwanted types.

Anyway, using cgi-lib.pl or the CGI_Lite module:

# get the file from the input stream
$upload = $in{"UPLOAD"};

# check if it's there, and write it to disk
if ($upload)
  # if it's a gif
  if ($upload=~/GIF/)
    open (IMAGE, ">filename.gif") || die "$!";
    unless (flock (IMAGE, LOCK_EX)) {die "$!"}
    print IMAGE $upload;

  # if it's plain text
    open (TEXT, ">filename.txt") || die "$!";
    unless (flock (TEXT, LOCK_EX)) {die "$!"}
    print TEXT $upload;

This, of course, assumes that you are only accepting gifs or plain text.  You can actually upload any filetype you want.  You have to figure out how to determine it's type though.  For instance, gifs have "GIF" in their header.  You also have to figure out how to name the file, either by a common system or let the user determine a name (potentially dangerous).  You don't have to save the file either... you could print it to the screen.  It's just a string containing the file contents, so you could print plain text directly, or you could put an image into the src of an image tag.  Or you could perform computations and alterations to it before printing or saving it.

I hope this answers your question.

Tom Anderson
CEO, Order amid Chaos, Inc.

RE: cgi script to upload a file and use it in a perl script

thanks for the help i will try using this.

RE: cgi script to upload a file and use it in a perl script

Hi ,i tried it but it does not even display the stuff on the web page
html file<!Form to upload a text file to generate a script .>
<TITLE>Uploading File</TITLE>
<FONT size=6 type="ariel" color = #FF0000 align ="center">Please click browse to Upload the File</FONT>
<TABLE width="510" border="0" cellpadding="0" cellspacing="0" bgcolor="#000080">
 <tr><td align="left" valign="top" height="20">
<FORM METHOD = POST ACTION ="http://sandbox.storyserver.zdnet.com/karora/cgi-bin/cgiScript.pl"; ENCTYPE="multipart/form-data">

Local File: <INPUT TYPE ="file" value= "Browse " ACCEPT="text/plain,image/gif">

<tr><td align="left" valign="top" height="20"><br>
&#032;&#032;<INPUT TYPE = "submit" value ="Transfer File">
<INPUT TYPE = "reset" value= "Reset ">

then i tried some thing what Tom  suggested ,but it says that it can't find the link,but if i just print something from the link it works fine .

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close