emilford has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to add code to my current script that will allow for image uploads to the remote server. I have the Perl Cookbook and there is an example for an FTP Client. I modeled that code in a previous script and it worked fine. This time, however, one of the methods I use returns an error. Here's the code:

sub upload_image() { use Net::FTP; my $path = $_[0]; # path received from HTML form input - location o +f image on local machine my ($username, $password, $directory) = ("xxxx", "xxxx", "/www/path/to +/store/files"); my $ftp = Net::FTP->new("www.xxxxxxxx.net", Timeout => 30, Debug => 1) || die &show_error("Unable +to connect to ftp: $!"); $ftp->login($username, $password) || die &show_error("Unable to lo +gin to ftp: $!"); $ftp->cwd($directory) || die &show_error("Unable to change directo +ry: $!"); $ftp->put($path) || die &show_error("Unable to upload $path: $!"); $ftp->quit(); }
When I run this and make a call to this sub routine the show_error function prints the error "No such directory". Is this referring to the local or remote server? Everything else prior to file upload works. What am I doing wrong?

I know that there are other methods (ie - cgi-lib, Cgi::Upload), but I haven't had much success with getting those to work how I'd like.

Any suggestions or code would be cool.

Replies are listed 'Best First'.
Re: Image Upload
by rob_au (Abbot) on Apr 03, 2002 at 07:16 UTC
    Okay ... There seems to be a couple of issues here - The first is the location of the file which you are looking to upload. From the code snippet you have given us, it would appear that the file is already on the web server and it from from there that it is to be uploaded elsewhere - This however, is not what I think you are wanting to achieve. From comments in your code, it looks like you are wanting to upload a file from the web client's machine to the web server, something which the code above is unable to perform - The reason for this is because this CGI code when executed is executed on the web server and not on the client's machine. As such, all references to files residing on the client system are invalid.

    As the author of CGI::Upload however, I would think that it should be able to do exactly what you desire - For example, the snippet of code returns a file handle to the file uploaded through the file input field named 'form_file_upload_field' on your HTML form:

    use CGI; use CGI::Upload; my $cgi = CGI->new; my $upload = CGI::Upload->new($cgi); my $fh = $upload->file_handle('form_file_upload_field');

    In addition to this, example usage of this module can be seen in this thread - If you have any questions regarding the CGI::Upload module, please feel free to contact me via email or /msg.

     

      use CGI::Simple qw(-upload); my $q = new CGI::Simple; my $ok = $q->upload( $q->param('upload_file'), '/path/to/write/file.na +me' ); print "Uploaded ".$q->param('upload_file')." and wrote it OK!" if $ok;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      If I understand the original intentions right, the author tries to ftp-upload the newly-http-uploaded files. We used this technique once to achieve more security. The problem was that the uploading (or better say 'handling-the-form-with-a-file-attached') CGI script was running as user www. That's the usual case under Apache without suExec. So we had to permit www user to write to the directory where user-uploaded files were to reside.
      And we used another idea instead. When we get a http-uploaded file somewhere in the /tmp dir (CGI.pm scripts usually do), we just ftp-upload it to our own directory. This method let us not to give away our own, say, /uploaded-images dir to www user, which, of course, was the owner of all CGI processes on that machine.
      Thanks for the reply and sort of pointing out my mistake. So basically, the error that I'm getting "No such directory" is referring to the directory on the remote server, right?

      I went ahead and looked at the threads you recommened and started going through some of the code. The code that you mentioned above, creates the file handle...can that then be used with my $ftp->put('the file handle')?

      Sorry if this question is trivial, but I'm relatively new to perl. I guess asking these questions at perlmonk.org helps me to learn new things....with perl, there certainly is always something I didn't know before!!!

      Any further insight on creating a !simple! upload script? Thanks in advance.