1

I am able to create my directory but I cannot seem to place the file in the directory.

#!/usr/bin/perl

use Cwd;
use CGI;

my $dir = getcwd();
print "Current Working Directory: $ dir\n";

my $photoDir = "$dir/MyPhotos";

mkdir $photoDir
        or die "Cannot mkdir $photoDir: $!"
        unless -d $photoDir;
        
        
my $query = new CGI;
my $filename = $query->param("Photo");
my $description = $query->param("description");

print "Current filename: $filename\n";

my ( $name, $path, $extension ) = fileparse ( $filename, '\..*' ); $filename = $name . $extension;
print $filename;
my $upload_filehandle = $query->upload("Photo");



open ( UPLOADFILE, ">$photoDir/$filename" )
 or die "$!"; 
binmode UPLOADFILE; 

while ( <$upload_filehandle> ) 
{ print UPLOADFILE; } 
close UPLOADFILE;

The CGI stack trace shows no errors but the log shows there is no output

LOG: 5 5020-0:0:0:0:0:0:0:1%0-9: CGI output 0 bytes.
3
  • Can you include Data::Dumper and dump out to the console what $upload_filehandle is, and post that to your question? (You want the original ">" append mode for open, as well.) Commented Mar 19, 2012 at 1:05
  • i am afraid i dont understand how to execute that 'Data : Dumper", i am running this as a localhost perl cgi and the only i can see is the log file. Is there a way to print $upload_filehandle Commented Mar 19, 2012 at 2:28
  • search.cpan.org/~smueller/Data-Dumper-2.131/Dumper.pm - it is included in the newer versions of perl, and it works wonders for debugging, as it can be used to print complex data structures to STOUT. Commented Mar 19, 2012 at 2:33

1 Answer 1

1

Update: This answer is outdated, today you will get an IO::File compatible handle directly:

# undef may be returned if it's not a valid file handle
if ( my $io_handle = $q->upload('field_name') ) {
    open ( my $out_file,'>>','/usr/local/web/users/feedback' );
    while ( my $bytesread = $io_handle->read($buffer,1024) ) {
        print $out_file $buffer;
    }
}

Please see the updated documentation.


Original answer:

CGI.pm manual suggests this path to saving uploaded files. Try this additional check and write method and see if it helps.

     $lightweight_fh  = $q->upload('field_name');

     # undef may be returned if it's not a valid file handle
     if (defined $lightweight_fh) {
       # Upgrade the handle to one compatible with IO::Handle:
       my $io_handle = $lightweight_fh->handle;

       open (OUTFILE,'>>','/usr/local/web/users/feedback');
       while ($bytesread = $io_handle->read($buffer,1024)) {
         print OUTFILE $buffer;
       }
     }

Also make sure you have your HTML form has required type like this: <form action=... method=post enctype="multipart/form-data">

Sign up to request clarification or add additional context in comments.

2 Comments

This is temporary variable to hold data in. You can define it just before the while loop as 'my $buffer;'
That worked but i did not end up using the buffer, the problem was that i did not have permissions to write to that directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.