Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the reason when ftp_put fails?

Tags:

php

ftp

I have a FTP class that has function in connecting, disconnecting, uploading and downloading files to the other ftp server.

I have this function and I wanted to log the reason why the upload fails in a text file, but based on the ftp_put docs, it only returns false on failure:

public function upload($remote_file, $file, $mode = FTP_ASCII) 
{
    if (!ftp_put($this->ftp_connection, $remote_file, $file, $mode)) 
    {
        throw new Exception("There was a problem while uploading $file", 1);
    }

    $this->last_uploaded_file = $file;
    return true;
} 

Is there any way to get the reason of failure for ftp_put? And what are those reasons? Or the only error message I could log is something like a generic message?:

Error uploading file Foo.bar 12:01:01 2015-01-01

like image 732
Ceeee Avatar asked May 06 '15 03:05

Ceeee


1 Answers

PHP FTP functions issue a warning with the last error message returned by the server.

So when this happens:

> STOR /nonexistingfolder/readme.txt  
< 550 Filename invalid

You get a warning like:

Warning: ftp_put(): Filename invalid in test.php on line 12

There's no more information provided. Particularly you cannot enable any kind of FTP session logging.


When there's a local problem (like a non-existing local file or permissions issue), you get a warning, as with any other PHP function:

Warning: ftp_put(nonexisting.txt): failed to open stream: No such file or directory in test.php on line 12

like image 91
Martin Prikryl Avatar answered Oct 20 '22 06:10

Martin Prikryl