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
(OP)
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
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
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..... ;^)
Second,
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'.
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
RE: cgi script to upload a file and use it in a perl script
<INPUT TYPE="FILE" SIZE="30" NAME="UPLOAD" ACCEPT="text/plain,image/gif">
</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{"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 "$!";
binmode(IMAGE);
unless (flock (IMAGE, LOCK_EX)) {die "$!"}
print IMAGE $upload;
close(IMAGE);
}
# if it's plain text
else
{
open (TEXT, ">filename.txt") || die "$!";
unless (flock (TEXT, LOCK_EX)) {die "$!"}
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 "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.
Sincerely,
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com
RE: cgi script to upload a file and use it in a perl script
RE: cgi script to upload a file and use it in a perl script
html file<!Form to upload a text file to generate a script .>
<HTML>
<HEAD>
<TITLE>Uploading File</TITLE>
</HEAD>
<BODY>
<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">
</td></tr><br>
<tr><td align="left" valign="top" height="20"><br>
  <INPUT TYPE = "submit" value ="Transfer File">
<INPUT TYPE = "reset" value= "Reset ">
</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 .