函数 glob() 不适用于外部 URL

Function glob() not working with externe URL

我对这个问题很困惑 当我在本地工作时,一切都很好 有了这个 :
foreach (glob("public/FolderA/B/") as $filename) {
但是当我把
foreach (glob("http://www.exemple.com/public/FolderA/B/") as $filename) {
没有任何解决方案???
History : 过去我使用 glob() 并与 local server 通信,而 scripte 完美地完成了这项工作,现在 donne 是 transfert 到其他服务器,我遇到的问题是如何让 glob() 工作 与外部 URL 而不是本地 或某些功能具有相同的功能

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
}

?>

如果您的 PHP 脚本是 运行 在 Windows 下,您可以使用

glob("\\remoteServer\public\FolderA\B\*.*")

因为正如 hherger 所说,"directory / files to be examined must be accessible via the server's file system." 和 Windows 允许使用 UNC 路径访问其他 PC。因为 Windows 使用反斜杠而不是正斜杠,并且在 PHP 中反斜杠是转义字符,所以每个反斜杠前面必须有一个反斜杠。在 Windows 资源管理器(又名文件资源管理器)中,上述 UNC 路径为

\remoteServer\public\FolderA\B\*.*