CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
uploading

 



parham_m_s
journeyman

Jan 13, 2001, 2:39 PM

Post #1 of 7 (640 views)
uploading Can't Post

Does anyone have a simple upload parser. Just something simple that doesn't use modules or anything fancy. Any help? Thanks in advance =).



Jasmine
Administrator / Moderator

Jan 14, 2001, 2:44 PM

Post #2 of 7 (631 views)
Re: uploading [In reply to] Can't Post

There's lots of programs that perform uploading functions at http://perlarchive.com/guide/File_Uploading



parham_m_s
journeyman

Jan 14, 2001, 6:52 PM

Post #3 of 7 (626 views)
Re: uploading [In reply to] Can't Post

i've looked, and all of them use modules



Fataqui
Deleted

Jan 15, 2001, 2:36 AM

Post #4 of 7 (621 views)
Re: uploading [In reply to] Can't Post

I can give you a old one I dont use anymore but it works very well on NT or UNIX It allows for only on file upload at a time but you can change this real easy!
When you upload you can change the name of the file you are uploading...

NOTE!!!!! This is old but it works great so no flaming from anyone please.

################HTML FORM###############

<html>
<head>
<title>Sample File Upload Form</title>
</head>
<body BGCOLOR=#808080 TEXT=#00FF00>

<CENTER><h1>Sample File Upload Form</h1></CENTER>

<CENTER><TABLE BORDER=0 WIDTH=550><TR><TD>
This page allows you to upload a file. Note your
Server might limit the size of POST <BR>content (so that
your server doesn't get swamped with data). This parameter
is <BR>configurable in the <I>config</I> file (<B>Maximum Content-Length</B>).
Only localhost <BR>(127.0.0.1) is permitted to upload scripts
(see upload.pl to customize this).
<P>
The file upload form looks just like any other form except that:
<UL>
<FONT COLOR=#FFFF00><LI> The form tag must specify the <tt>POST</tt> method
<LI> The form tag must specify an enctype of <tt>multipart/form-data</tt>
<LI> The form must contain an <tt><input type=file></tt> element.
<LI> The form must point to the Upload.pl script.</FONT>
</UL>
<P>
You can do a view source on this document to see all the elements.
Uploaded <BR>files are placed in the directory you select in the ( upload.pl ) script.
<P>
<HR>
<H2>File Upload Form</H2>
</TD></TR></CENTER></TABLE>
<FORM method=POST action="/cgi-bin/upload.pl" enctype="multipart/form-data">
<CENTER>
<TABLE BORDER=0 WIDTH=550>
<TR>
<TD>File to upload</TD>
<TD><input type=file size=45 name=upfile></TD>
</TR>
<TR>
<TD>File name</TD>
<TD><input type=text value="test" name=upname></TD>
</TR>
<TR>
<TD></TD>
<TD><input type=submit value=Upload></TD>
</TR>
</CENTER></TABLE>
<P>
</FORM>
</body>
</html>

###########END OF HTML FORM############


############INSTALL NOTES##################

Upload Perl Based Script






This is a very easy script to install just upload ( Upload.pl ) to your cgi directory.
Remember if you are uploading this script and html files you must upload them to your
server ASCII do not upload them from a PC to a Unix server without using some sort of
ftp file transfer software because you will be uploading in BINARY and the script will not
work when done that way. Also upload your form Upload.htm the same way. You should make
your own form and upload it to a directory on your server that you can call it from.

When uploading to a UNIX server you must "chmod 755" the Upload.pl script also if your
server doesn't except . ( PL ) extension change it to ( CGI ). The reason for "chmod 755"
for the script is that you give your self the owner ( read/write/execute ) and all others
can just read/execute.

The path to your upload directory on a UNIX server must start with a ( / ) and end with one
also ( / ). This is not always the case with the ending ( / ) but for this script it is
that way. It would look like this.

$up_dir = "/usr/www/user/upstate/Upload/";

For windows systems the path would be some what different. If you are hosting a server on
your windows system you will have to write this way.

$up_dir = "C:/server-file/server-root-directory/Upload/";

Another easy way for windows users is this way.

$up_dir = "../server-root-directory/Upload/";


Also before uploading make the nessary changes to the Upload.pl script. Change where your
perl is located and also change where your upload ( $up_dir ) directory will be on your
server. Then if you want to be the only one with permission to upload using this script
change the ( Remote_Host ) to your IP. You should not have to change this if you own the
server and you are entering the server as the localhost but if you want to use this script
remotely change the variable to your IP this way the script will check your IP before it
run's any uploads. If you want anyone to have permission to upload files then comment the
lines in the Upload.pl script where it says to do so.

After that you should be good to go.

############END OF INSTALL NOTES###########


############UPLOAD.PL####################

#!/usr/local/bin/perl

# Perl-based Upload Script
# Version 1.0

##########################################
######## Configuration Settings ##########
# Above point this script to your perl #
############### directory ################
# Below the directory you want the files #
# to be uploaded to, it must be the true #
########### path not the url #############
# It would look like this on a windows #
############## based system ##############
######## C://Server/docs/upload/ #########
# Upload Directory ( Remember to include the trailing [ / ] #
$up_dir = "/usr/www/users/upstate/docs/upload/";
#
# PARSE THE CGI FORM
#
$content_type = $ENV{'CONTENT_TYPE'};
$content_len = $ENV{'CONTENT_LENGTH'};
$host_test = $ENV{'REMOTE_ADDR'};

# Only allow localhost to upload.
# Leave this if you only want your self to be able to upload to your server.
# Remove it or comment all lines down to [Buffer the Post content]
# if you want any one to upload to your server.
# If you allow anyone to upload it is safe because
# only the directory you listed above will be used to upload.
# No one can delete or change anything in the upload directory
# once it is uploaded.
# If you are uploading by remote access change
# the host address below to your [ IP ] address.
if (!($host_test eq '127.0.0.1'))
{
print "Only localhost is allowed to use this script!\n";
exit(1);
}

# Buffer the POST content
binmode STDIN;
read(STDIN, $buffer, $content_len);

if ((!$content_type) ||
($content_type eq 'application/x-www-form-urlencoded'))
{
# Process the name=value argument pairs
@args = split(/&/, $buffer);

$data = '';
foreach $pair (@args)
{
($name, $value) = split(/=/, $pair);

# Unescape the argument value
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

# Save the name=value pair for use below.
$FORM{$name} = $value;
}
}
elsif ($content_type =~ m#^multipart/form-data#)
{
# find boundary
# This is to allow for quotes.
#
# ($boundary = $content_type) =~ s/^.*boundary=(.*)$/\1/;
($boundary = $content_type) =~ s/^.*boundary="?(.*?)"?$/\1/;

@pairs = split(/--$boundary/, $buffer);
@pairs = splice(@pairs,1,$#pairs-1);

for $part (@pairs)
{
($dump,$fline,$value) = split(/\r\n/,$part,3);
next if $fline =~ /filename=\"\"/;
$fline =~ s/^Content-Disposition: form-data; //;
(@columns) = split(/;\s+/, $fline);
($name = $columns[0]) =~ s/^name="([^"]+)"$/\1/g;

if ($#columns > 0)
{
if ($value =~ /^Content-Type:/)
{
($dump,$dump,$value) = split(/\r\n/,$value,3);
}
else
{
($dump,$value) = split(/\r\n/,$value,2);
}
}
else
{
($dump,$value) = split(/\r\n/,$value,2);
if (grep(/^$name$/, keys(%CGI)))
{
if (@{$FORM{$name}} > 0)
{
push(@{$FORM{$name}}, $value);
}
else
{
$arrvalue = $FORM{$name};
undef $FORM{$name};
$FORM{$name}[0] = $arrvalue;
push(@{$FORM{$name}}, $value);
}
}
else
{
next if $value =~ /^\s*$/;
$FORM{$name} = $value;
}
next;
}

$FORM{$name} = $value;
}
}
else
{
print "Invalid content type!\n";
exit(1);
}

#
# VERIFY THE FORM DATA
#
$upfile = $FORM{'upfile'};
$upname = $FORM{'upname'};
if (!($upfile) || !($upname))
{
print "<HTML><TITLE>Missing fields</TITLE><BODY>\n";
print "No upload file specified!\n";
print "</BODY></HTML>\n";
exit(1);
}


#
# CLOSE SECURITY PROBLEMS.
#
if ($upname =~ /[;><&\*'\|\/\\]/ )
{
print "<HTML><TITLE>Invalid file name</TITLE><BODY>\n";
print "The upload file name is invalid.\n";
print "</BODY></HTML>\n";
exit(1);
}

#
# Write out the upload file
#
$filename = "$up_dir".$upname;
open(FILE, ">$filename") || exit(1);
binmode FILE;

print FILE $upfile;
close FILE;


#
# DONE
#
print "Upload of ".$upname." succeeded.\n";

exit(0);

########END UPLOAD.PL#################



Cure
User

Jan 16, 2001, 9:30 PM

Post #5 of 7 (604 views)
Re: uploading [In reply to] Can't Post

Why not use a Module ?

use CGI.pm


Cure



japhy
Enthusiast

Jan 17, 2001, 4:50 AM

Post #6 of 7 (599 views)
Re: uploading [In reply to] Can't Post

Your request to have a program that does not use modules is errant and bogus. You should have CGI.pm, since it is a standard module. That means you should be able to create a form, with an enctype of "multipart/form-data", and a file upload field, and then use CGI.pm's simple interface to get the uploaded file.

If you want to know HOW this works, I can tell you right now that it is complex, and you are probably not able to handle the parsing on your own.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


parham_m_s
journeyman

Feb 1, 2001, 12:34 PM

Post #7 of 7 (560 views)
Re: uploading [In reply to] Can't Post

You might find this absolutely ridiculous japhy, but i believe that if i don't know how something works, i shouldn't try to find a shortcut, i actually want to learn how to start uploading files, that's why i wanted to take a look at other scripts to observe and learn.

well, you've given me that extra push to put all other programming aside and try to learn to upload files in perl.

I've gotten the easy part done, set up the forms (multiple files), and the basic perl. I print the buffer to a temporary text file, but how would i split it so that each file is separated. I tried splitting myself, but then i couldn't take out the specific filename and strip the extra stuff. Any help?


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives