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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading with CGI.pm

Status
Not open for further replies.
Joined
Jun 19, 2001
Messages
86
Location
US
I need a very simple script that I can use to upload images to a directory on a Unix server. I have tried:

$image_dir = "/mywebsite/imagedir/uploads/";
$upload_image = param('photo');
open (UPLOADIMAGE, ">>$image_dir");
while ($bytesread=read($upload_image,$buffer,1024))
{
print UPLOADIMAGE $buffer;
}

but it doesn't work. Any help would be greatly appreciated.


Please pray for our country and it's leaders.
 
Does this help?

$image_dir = "/mywebsite/imagedir/uploads/";
$upload_image = param('photo');
open (UPLOADIMAGE, ">>$image_dir");
binmode(UPLOADIMAGE);
print UPLOADIMAGE <$upload_image>;

 
I tried that too with no luck. Thanks for the suggestion. I read that Unix ignores the

binmode(UPLOADIMAGE);

section.
 
please take a look at the faq, faq219-406. It contains an anotated example of doing this trick.

HTH

ps - there is another faq about doing it with cgi-lib.pl, in case you are not using CGI.pm Please use descriptive titles and check the FAQs.
And, beware the evil typo.
 
This is a subroutine that I use, you have to have a param provide it the information to write from.

#These two statements will produce a upload input form field in your PERL Script generated form...

print &quot;<center>Enter or Browse to the file you would like to upload.<BR>\n&quot;;
print $query->filefield(-name=>&quot;fileID&quot;, -size=>50, -maxlength=>80);


#This is the variable that your are going to read data from..
$fileID = $query->param('fileID');

#This is the directory you are going to write to

$data_dir=&quot;your/directory/on/your/server&quot;;

#This is the subroutine that will read $fileID and print it #two a new file on the server. You must envoke this #routine. usually with an if command.

sub upload{
#put file name in proper fomant
@pathName=split(/\\/,$fileID);
#clip off the last name in the array
$addfile=pop(@pathName);
#define new directory for file to go
$newFile = &quot;$data_dir$addfile&quot;;

# open a handle to the file you will write
open(OPF,&quot;>$newFile&quot;) || &showError(&quot;Failed to open OPF, $!&quot;);

# while you read from the remote machine, write to the output file.
while (read($fileID,$buffer,1024)) { print OPF &quot;$buffer&quot;};
#this will allow only certain file types
#follow the format shown to allow diffent file types
unless ($addfile =~ /jpg|gif/i)
{
unlink $newFile;
#you will have to write a subroutine to create or point to an error page
&showError(&quot;Dangerous file type.<BR>Deleted file.&quot;);
}

close OPF;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top