Jump to content

if ftp_get does not find the file to download echo error message and exit script


kk4iku

Recommended Posts

I am downloading a file from an FTP site and sometimes the file will not be there how can I exit the PHP gracefully ?

 

If there is not a file I get the below error message.

 

ftp_get(): Requested action not taken. Message not found. 

 

 Error downloading sample.txt

 

if (ftp_get($ftp_conn,  $local_file, $ftp_path.$server_file, FTP_ASCII))
  {


  echo "Successfully written to $local_file.";
  }
else
  {
  echo "Error downloading $server_file.";
  ftp_close($ftp_conn);
  exit;
  }


ftp_close($ftp_conn);

 

Link to comment
Share on other sites

Make your code smart enough to know if the file exists: use ftp_nlist to get the contents of the parent directory, then check that the file requested is in there.

 

You should also change your production environment (the php.ini/whatever settings) to log errors (set error_log if not already set) instead of displaying them (turn off display_errors).

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.