Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the FTP error when using PHP

Tags:

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

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

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:

echo "There was a problem while renaming $old_file to $new_file:  the server says $error_message\n"; 
like image 792
AmbroseChapel Avatar asked Nov 11 '08 04:11

AmbroseChapel


People also ask

What is FTP PHP?

PHP FTP Introduction The FTP functions give client access to file servers through the File Transfer Protocol (FTP). The FTP functions are used to open, login and close connections, as well as upload, download, rename, delete, and get information on files from file servers.


2 Answers

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

like image 116
Sascha Schmidt Avatar answered Sep 24 '22 03:09

Sascha Schmidt


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.

Use error_get_last() instead.

See answer by @Sascha Schmidt

like image 34
Peter Hopfgartner Avatar answered Sep 20 '22 03:09

Peter Hopfgartner