当 ftp_put 失败时,有没有办法得到原因?

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

我有一个 FTP class 可以连接、断开连接、上传和下载文件到另一个 ftp 服务器。

我有这个功能,我想在文本文件中记录上传失败的原因,但是根据 ftp_put 文档,失败时它只有 returns false:

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;
} 

有什么方法可以得到ftp_put失败的原因吗?这些原因是什么?或者我可以记录的唯一错误消息类似于一般消息?:

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

PHP FTP 函数使用服务器返回的最后一条错误消息发出警告。

所以当这种情况发生时:

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

您会收到如下警告:

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

没有提供更多信息。特别是您不能启用任何类型的 FTP 会话日志记录。


当出现本地问题(例如不存在的本地文件或权限问题)时,您会收到警告,就像任何其他 PHP 函数一样:

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