php 使用PHP时如何获取FTP错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/280014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:13:34  来源:igfitidea点击:

How to get the FTP error when using PHP

phperror-handlingftp

提问by AmbroseChapel

I have a script which logs on to a remote server and tries to rename files, using PHP.

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

The code currently looks something like this example from the php.net website:

该代码目前类似于 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";
}

but ... what was the error? Permissions, no such directory, disk full?

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

How can I get PHP to return the FTP error? Something like this:

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

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

采纳答案by FlySwat

Looking at the FTP API here:

在此处查看 FTP API:

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

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

There doesn't seem to be any way to get anything but true or false.

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

However, you could use ftp_raw to send a raw RENAME command, and then parse the returned message.

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

回答by Sascha Schmidt

You could use error_get_last() if return value is false.

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

回答by Peter Hopfgartner

I'm doing something like:

我正在做类似的事情:

$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);

EDIT:

编辑:

Note that $php_errormsg is deprecated as of PHP 7.

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

Use error_get_last() instead.

改用 error_get_last() 。

See answer by @Sascha Schmidt

请参阅@Sascha Schmidt 的回答

回答by jsherk

Based on @Sascha Schmidt answer, you could do something like this:

根据@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.

print_r 将显示 error_get_last() 数组的内容,以便您可以查明错误。