Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

kavi98

Programmer
Dec 14, 2000
48
US
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.

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

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

Finally,
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
 
<FORM METHOD=&quot;POST&quot; ACTION=&quot;script.pl&quot; ENCTYPE=&quot;multipart/form-data&quot;>
<INPUT TYPE=&quot;FILE&quot; SIZE=&quot;30&quot; NAME=&quot;UPLOAD&quot; ACCEPT=&quot;text/plain,image/gif&quot;>
</FORM>

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{&quot;UPLOAD&quot;};

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

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

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 &quot;GIF&quot; 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.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
thanks for the help i will try using this.
 
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 .>
<HTML>
<HEAD>
<TITLE>Uploading File</TITLE>
</HEAD>
<BODY>
<FONT size=6 type=&quot;ariel&quot; color = #FF0000 align =&quot;center&quot;>Please click browse to Upload the File</FONT>
<TABLE width=&quot;510&quot; border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; bgcolor=&quot;#000080&quot;>
<tr><td align=&quot;left&quot; valign=&quot;top&quot; height=&quot;20&quot;>
<FORM METHOD = POST ACTION =&quot; ENCTYPE=&quot;multipart/form-data&quot;>


Local File: <INPUT TYPE =&quot;file&quot; value= &quot;Browse &quot; ACCEPT=&quot;text/plain,image/gif&quot;>
</td></tr><br>

<tr><td align=&quot;left&quot; valign=&quot;top&quot; height=&quot;20&quot;><br>
&amp;#032;&amp;#032;<INPUT TYPE = &quot;submit&quot; value =&quot;Transfer File&quot;>
<INPUT TYPE = &quot;reset&quot; value= &quot;Reset &quot;>
</td></tr>
</FORM>
</TABLE>
</BODY></HTML>

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 .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top