Amb*_*pel 19 php ftp error-handling
我有一个脚本登录到远程服务器并尝试使用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";
}
Run Code Online (Sandbox Code Playgroud)
但是......错误是什么?权限,没有这样的目录,磁盘已满?
如何让PHP返回FTP错误?像这样的东西:
echo "There was a problem while renaming $old_file to $new_file:
the server says $error_message\n";
Run Code Online (Sandbox Code Playgroud)
小智 33
如果返回值为false,则可以使用error_get_last().
在这里查看FTP API:
http://us.php.net/manual/en/function.ftp-rename.php
除了真或假,似乎没有办法得到任何东西.
但是,您可以使用ftp_raw发送原始RENAME命令,然后解析返回的消息.
小智 9
我做的事情如下:
$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);
Run Code Online (Sandbox Code Playgroud)
根据 @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
}
Run Code Online (Sandbox Code Playgroud)
print_r 将显示 error_get_last() 数组的内容,以便您可以查明错误。