glob() 根据定义查找与模式匹配的路径名。
这意味着该函数将无法在远程文件上运行,因为要检查的目录 / 文件必须可以通过服务器的文件系统访问。
您可能需要的是通过 FTP 服务器访问远程文件系统。
这就是它的样子:
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
get contents of the current directory
$contents = ftp_nlist($conn_id, "."); // "." means the current directory
var_dump($contents);
或者,如果之前的本地服务器仍然可以访问,您可以在此服务器上设置一个脚本,像以前一样扫描目录并回显文件列表(例如,以 XML 或 JSON 格式)。该脚本可以由(现在)远程脚本发送请求,以这种方式提供文件列表。
更新:FTP 访问,完整脚本
<?php
$ftp_server = 'ftp.example.org';
$ftp_port = 21;
$ftp_timeout = 90;
$ftp_user = 'my_username';
$ftp_password = 'my_password';
// set up a connection or die
$conn_id = ftp_connect($ftp_server, $ftp_port, $ftp_timeout);
if ($conn_id===false) {
echo 'Failed to connect to the server<br />';
exit(1);
}
// Log in or die
$logged_in = ftp_login($conn_id, $ftp_user, $ftp_password);
if ($logged_in!==true) {
echo 'Failed to log-in<br />';
exit(1);
}
// Change directory if necessary
echo "Current directory: " . ftp_pwd($conn_id) . '<br />';
// Set to passive mode if required
ftp_pasv ($conn_id, true);
// Change directory if necessary
if (ftp_chdir($conn_id, 'subdir1/subdir2')) {
echo "Current directory is now: " . ftp_pwd($conn_id) . '<br />';
} else {
echo 'Could not change directory<br />';
exit(1);
}
// Get list of files in this directory
$files = ftp_nlist($conn_id, ".");
if ($files===false) {
echo 'Failed to get listing<br />';
exit(1);
}
foreach($files as $n=>$file) {
echo "$n: $file<br />";
$local_dir = '/my_local_dir/';
foreach($files as $n => $file) {
// These we don't want to download
if (($file=='.') || ($file=='..') || ($file[0]=='.')) continue;
// These we do want to download
echo "$n: $file<br />";
if (ftp_get($conn_id, $local_dir.$file, $file, FTP_BINARY)) {
echo "Successfully written to $local_dir$file<br />";
} else {
echo "Could not get $local_dir.$file<br />";
}
}
// Do whatever has to been done with $file
}
?>