Jump to content

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


kk4iku
Go to solution Solved by requinix,

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

  • Solution

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).

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.