I use this code to access directory.
$location = 'files/';
$pictures = glob($location . "*.png");
I want to access remote path using FTP
$location = opendir('ftp://user:password@host_name/files');
$pictures = glob($location . "*.png");
My FTP directory access code not work correctly.
PHP glob function does not support URL wrappers:
Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.
The only reliable way is to list files matching a wildcard, is to list all files using PHP FTP functions and filter them locally:
$conn_id = ftp_connect("ftp.example.com") or die("Cannot connect");
ftp_login($conn_id, "username", "password") or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot change to passive mode");
$files = ftp_nlist($conn_id, "/path");
foreach ($files as $file)
{
if (preg_match("/\.png$/i", $file))
{
echo "Found $file\n";
}
}
(this is what the glob would do internally anyway, had it supported URL wrappers)
Some (most) FTP servers will allow you to use a wildcard directly:
$conn_id = ftp_connect("ftp.example.com") or die("Cannot connect");
ftp_login($conn_id, "username", "password") or die("Cannot login");
ftp_pasv($conn_id, true) or die("Cannot change to passive mode");
$files = ftp_nlist($conn_id, "/path/*.png");
foreach ($files as $file)
{
echo "Found $file\n";
}
But that's a nonstandard feature (while widely supported).
For details see my answer to FTP directory partial listing with wildcards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With