【问题标题】:Is there a way to get the reason when ftp_put fails?当 ftp_put 失败时,有没有办法得到原因?
【发布时间】:2015-05-06 03:27:50
【问题描述】:

我有一个 FTP 类,它具有连接、断开连接、上传和下载文件到其他 ftp 服务器的功能。

我有这个功能,我想在文本文件中记录上传失败的原因,但根据ftp_put 文档,它只会在失败时返回 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的失败原因吗?这些原因是什么?或者我可以记录的唯一错误消息类似于通用消息?:

上传文件 Foo.bar 2015-01-01 12:01:01 时出错

【问题讨论】:

    标签: php ftp


    【解决方案1】:

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

    所以当这种情况发生时:

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

    您会收到如下警告:

    警告:ftp_put():文件名在第 12 行的 test.php 中无效

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


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

    警告:ftp_put(nonexisting.txt):打开流失败:第 12 行的 test.php 中没有这样的文件或目录

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2011-03-08
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      相关资源
      最近更新 更多