24

我有一个脚本,它登录到远程服务器并尝试使用 PHP 重命名文件。

该代码目前看起来类似于 php.net 网站上的这个示例:

if (ftp_rename($conn_id, $old_file, $new_file)) {
 echo "successfully renamed $old_file to $new_file\n";
} else {
 echo "There was a problem while renaming $old_file to $new_file\n";
}

但是......错误是什么?权限,没有那个目录,磁盘满了?

如何让 PHP 返回 FTP 错误?像这样的东西:

echo "There was a problem while renaming $old_file to $new_file: 
the server says $error_message\n";
4

4 回答 4

39

如果返回值为 false,您可以使用 error_get_last()。

于 2012-01-13T08:44:11.007 回答
12

我正在做类似的事情:

$trackErrors = ini_get('track_errors');
ini_set('track_errors', 1);
if (!@ftp_put($my_ftp_conn_id, $tmpRemoteFileName, $localFileName, FTP_BINARY)) {
   // error message is now in $php_errormsg
   $msg = $php_errormsg;
   ini_set('track_errors', $trackErrors);
   throw new Exception($msg);
}
ini_set('track_errors', $trackErrors);

编辑:

注意 $php_errormsg 自 PHP 7 起已弃用。

请改用 error_get_last()。

查看@Sascha Schmidt 的回答

于 2012-10-16T08:29:10.767 回答
11

在这里查看 FTP API:

http://us.php.net/manual/en/function.ftp-rename.php

除了真假之外,似乎没有任何方法可以得到任何东西。

但是,您可以使用 ftp_raw 发送原始 RENAME 命令,然后解析返回的消息。

于 2008-11-11T04:39:47.787 回答
4

根据@Sascha Schmidt 的回答,您可以执行以下操作:

if (ftp_rename($conn_id, $old_file, $new_file)) {
 echo "successfully renamed $old_file to $new_file\n";
} else {
 echo "There was a problem while renaming $old_file to $new_file\n";
 print_r( error_get_last() ); // ADDED THIS LINE
}

print_r will display the contents of the error_get_last() array so you can pinpoint the error.

于 2019-06-21T21:30:06.843 回答