Do not use on any
shared computer
September 7, 2008 04:44am pdt
 
[x]
Attachment Details

Perl script that upload a file to the server

Tags: form, how, uplaods, upload
OK, I recently got this script from someone, it suppossedly uploads a file to a server. I am a web developer, who works with everything but perl and cgi scripting, so please dont think I am totally retarded.
Anyway, I put the script in the cgi - bin, I wrote a test page, it just gives me a 500 error. I am not quite sure how to debug it... Here is the perl script, if you have time to look at it



#!/usr/local/bin/perl



$|++;

print "Content-type: text/html\n\n";



use CGI;

$query=new CGI;



##     The directory on your server where you want to store the file!!

$upload_dir="/usr/local/etc/httpd/htdocs/gold";

$data_file="/tmp/data.txt";

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);



##     Pad the day and month with a 0

$mday=sprintf("%02d",$mday);

$mon=sprintf("%02d",(++$mon));

$year+=1900;



##     Pad the hour, minutes and with a 0

$hour=sprintf("%02d",(++$hour));

$min=sprintf("%02d",(++$min));

$sec=sprintf("%02d",(++$sec));



##     Create a date time stamp of the format YYMMDDHHMMSS

$datetime=$year.$mon.$mday.$hour.$min.$sec;



##     Process the incoming HTML variables

foreach($query->param){

     $FORM{$_}=$query->param($_);

     ##     Remove all leading and trailing spaces

     $FORM{$_}=~ s/^\s+//;

     $FORM{$_}=~ s/\s+$//;

}



##     Check the mandatory fields. They should not be empty

if (!$FORM{name}){

     print qq{<B>Name cannot be empty.</B><P>

Click on the Back button of your browser to enter a <B>Name</B><P><HR>\n};

     exit;

}



if (!$FORM{email}){

     print qq{<B>Email Id cannot be empty.</B><P>

Click on the Back button of your browser to enter a <B>Email Id</B><P><HR>\n};

     exit;

}



if (!$FORM{upfile}){

     print qq{<B>Please enter a File to Upload.</B><P>

Click on the Back button of your browser to enter a <B>File to Upload</B><P><HR>\n};

     exit;

}



$upfile=$FORM{'upfile'};

$upfile=~ s/\\/\//g;     ##     Replace all \'s with /'s.



##     Extract the upload file's name.

@path=split(/\//,$upfile);

$upload_file= $upload_dir."/".$path[$#path].".$datetime";

         

open(MYFILE,"> $upload_file") || die $!;

binmode MYFILE;

while($bytesread=read($FORM{'upfile'},$data,1024)){

     $size+=$bytesread;

     print MYFILE $data;

}

close(MYFILE);

close($FROM{'upfile'});

         

print qq{<B>$FORM{'upfile'}</B> has been uploaded.<P>

<!-- File saved as $upload_file -->

Size = $size byte(s)<P>

Click on the Back button of your browser to Upload a another file<HR>

};



##     Now write the users details to the data file

open (TXTFILE,">>$data_file")|| die "Cannot open $data_file for appending<P>\n";

## Exclusive lock (for writing)

unless (flock(TXTFILE, 2)) {

     die "Can't get write-lock on $path: $!. Try again later.";

}



##     Store the datetime stamp with the other user info.

push(@col,$datetime);

foreach(sort keys %FORM){

     push(@col,$FORM{$_});

}



##     Store the entry as a :: delimited entry

print TXTFILE (join(':',@col))."\n";

close(TXTFILE);




If u have anything simple that just takes a file and uplaods it, and nothing else at all, whatso-ever. Please help me out'
Thanks

p.s. sorry, I have so little points to give out
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: nywebsol
Solution Provided By: maneshr
Participating Experts: 3
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Expert Comment by maneshr

Rank: Wizard

Expert Comment by maneshr:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by nywebsol
Author Comment by nywebsol:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Accepted Solution by maneshr

Rank: Wizard

Accepted Solution by maneshr:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by holli

Rank: Master

Expert Comment by holli:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by holli

Rank: Master

Expert Comment by holli:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by maneshr

Rank: Wizard

Expert Comment by maneshr:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by maneshr

Rank: Wizard

Expert Comment by maneshr:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by Moondancer
Expert Comment by Moondancer:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by maneshr

Rank: Wizard

Expert Comment by maneshr:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by Moondancer
Expert Comment by Moondancer:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34