Search code examples
phpfunctionftpclient-serverglob

Function glob() not working with externe URL


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


Solution

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