Reputation: 326
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.
Upvotes: 4
Views: 3135
Reputation: 202262
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.
Upvotes: 6