2

I want to check availability of the files in the FTP server. If not exit then an automation mail will be sent. This code is working fine for an exact file name to be give in the variable, but I want to use (%) to show as this : $filename = 'temp%', to match only the first part of the file name before percentage. How to use this in PHP?

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

$filename = 'temp%';
// try to check if files exist 
if (!file_exists($filename)) {

    //Sending Email
    $htmlbody ="
    <head>
    ...";
    // ...
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Meena
  • 55
  • 7

1 Answers1

0

You have to list remote files and scan the retrieved list for files matching your pattern.
See Listing files on FTP server using PHP glob is not working.


Some FTP servers do allow wildcard to be used directly with listing command. See FTP directory partial listing with wildcards (though note that it's a non-standard extension, while actually widely supported).

On such FTP servers, this will do:

if (empty(ftp_nlist($conn_id, "temp*")))
{
    echo "No such file";
}

If your FTP server does not allow wildcards, or if you do want to rely on a non-standard feature, you have to filter the files yourself:

if (empty(preg_grep("/^temp/", ftp_nlist($conn_id, ""))))
{
    echo "No such file";
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992