views:

152

answers:

4

I have this code:

use CGI;    
use File::Basename;    
use Image::Magick;    
use Time::HiRes qw(gettimeofday);    
my $query = new CGI;    
my $upload_dir = "../images"; #location on our server    
my $filename=$query->param("img");    
my $timestamp = int (gettimeofday * 1000);    
my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' );     
$filename = $name ."_".$timestamp. $extension;    

 #upload the image

my $upload_filehandle =$query->param("img");    
open ( UPLOADFILE, ">$upload_dir/$filename" ) or die "$!";  
 binmode UPLOADFILE;  
 while ( <$upload_filehandle> )  
  {  
   print UPLOADFILE;  
   }  

  close UPLOADFILE;

Then I do resize for the image. My problem is that the image is not uploaded. I have an empty file having the name of $filename. I omitted the part related to resize and I still have the code. So I'm sure that the file is not being uploaded correctly. Am I missing any library? Any help?Plz..

+2  A: 

You need to change

my $upload_filehandle =$query->param("img");

to

my $upload_filehandle =$query->upload("img");

according to this tutorial.

Hope it helps.

klausbyskov
It did not work. When I print "|$upload_filehandle|" i get it ||.which means that the uploaded file is empty. Any idea why?
zeina
+1  A: 

I figured out why: As pointed out in the CGI.pm docs, the form's encoding type must be multipart/form-data. When I added that, everything worked.

zeina
A: 

same issue with me too..

adding my code for ref

my $fileh=$cgi->upload('img1'); while(){ print $_; } close($fileh);

i am trying to just upload a txt file and print it..

ir

jabir
A: 

added this "ENCTYPE='multipart/form-data'" instead of encoding.

> <form action="/post.pl" method="post"  ENCTYPE='multipart/form-data'>

and it worked

jabir