Function glob() not working with externe URL

半世苍凉 提交于 2019-12-11 23:25:55

问题


i'm confused with this problem when im working locally it's all fine with this :
foreach (glob("public/FolderA/B/") as $filename) {
but when i put
foreach (glob("http://www.exemple.com/public/FolderA/B/") as $filename) {
doesn't work any solution ???
History : in the past i'm working with glob() and communicate with local server and the scripte do this jobs perfect now donne is transfert to other server and problem i get is how to put glob() working with external URL not locally or some function has same functionality


回答1:


glob() per definition finds pathnames matching a pattern.
This means that the function will not work on remote files as the diretory / files to be examined must be accessible via the server's filesystem.

What you probably would need is accessing a remote filesystem thru an FTP server.

This is how it could look like:

$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);

Alternatively, if the previously local server still can be accessed, you could have a script on this server scan the directory as before and echo the file list (e.g. in XML or JSON format). This script could the be sent a request by the (now) remote script, giving the file list this way.

Update: FTP Access, full script

<?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
}

?>



回答2:


If your PHP script is running under Windows, you can use

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

because as hherger said "directory / files to be examined must be accessible via the server's file system." and Windows allows access to other PCs using a UNC path. Because Windows uses backslashes instead of forward slashes, and in PHP a backslash is an escape character, each backslash has to be preceded by a backslash. In Windows Explorer (aka File Explorer), the above UNC path would be

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


来源:https://stackoverflow.com/questions/34882114/function-glob-not-working-with-externe-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!