Hi all, I am trying to connect to a remote server, I am using the IP address server name and folder to connect, not sure if ths is working?

I am aslo getting an error on the final foreach loop (foreach ($files as $file) {//print array) Warning: Invalid argument supplied for foreach() in C:\wamp\www\open_dir\index.php on line 50.

Any help would be received would be appriciated

Thanks in advance


function filelist ($startdir="./http://IPaddress/WATKINS-TS\Webserv/", $searchSubdirs=1, $directoriesonly=0, $maxlevel="all", $level=1) {
    //list the directory/file names that you want to ignore
    $ignoredDirectory[] = "."; 
    $ignoredDirectory[] = "..";
    $ignoredDirectory[] = "_vti_cnf";
    global $directorylist;    //initialize global array
    if (is_dir($startdir)) { 
        if ($dh = opendir($startdir)) { 
            while (($file = readdir($dh)) !== false) {
                if (!(array_search($file,$ignoredDirectory) > -1)) {
                 if (filetype($startdir . $file) == "dir") {
                       //build your directory array however you choose; 
                       //add other file details that you want.
                       $directorylist[$startdir . $file]['level'] = $level;
                       $directorylist[$startdir . $file]['dir'] = 1;
                       $directorylist[$startdir . $file]['name'] = $file;
                       $directorylist[$startdir . $file]['path'] = $startdir;
                       if ($searchSubdirs) {
                           if ((($maxlevel) == "all") or ($maxlevel > $level)) {
                               filelist($startdir . $file . "/", $searchSubdirs, $directoriesonly, $maxlevel, $level + 1);
                   } else {
                       if (!$directoriesonly) {
                           //if you want to include files; build your file array  
                           //however you choose; add other file details that you want.
                         $directorylist[$startdir . $file]['level'] = $level;
                         $directorylist[$startdir . $file]['dir'] = 0;
                         $directorylist[$startdir . $file]['name'] = $file;
                         $directorylist[$startdir . $file]['path'] = $startdir;
$files = filelist("./",1,1); // call the function
foreach ($files as $file) {//print array
    echo "Directory: " . $file['dir'] . " => Level: " . $file['level'] . " => Name: " . $file['name'] . " => Path: " . $file['path'] ."<br>";
3 Years
Discussion Span
Last Post by diafol

Hi diafol, thanks for you reply.

I have applied the code and put a forward / for root dir and it produced the root dir of my PC.

I removed all the other code and just placed your code in PHP file foreach (new DirectoryIterator('/') as $fileInfo) { if($fileInfo->isDot()) continue; echo $fileInfo->getFilename() . "<br>\n"; } and I get the same result.
My prime objective to to connect to a remote server, as this connects to my local server, how can I acheive this?

Appreciate your help on this

Thanks in advance


Edited by davidjennings


To access files on a remote server, your best bet is probably the FTP extension. The general filesystem functions only work with your local server/an HTTP request to a remote server, which won't give you the level of functionality you're looking for.



Sorry missed the remote server bit :(

No, you can't traverse the remote server with 'normal' functions. You can do it with ftp functions, or here's a simple script - think I got it from SO - not sure...

function retTxt($filename) {
    $content ='';
    $fp_load = fopen("$filename", "rb");
    if ( $fp_load ) {
            while ( !feof($fp_load) ) {
                $content .= fgets($fp_load, 8192);
            return $content;

$matches = array();
preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", retText('http://www.example.org/css'), $matches);

foreach($matches[2] as $match) echo $match . '<br/>';

Hi Diafol, thanks for the reply

I have added a link around echo $match on line 16 which produces a hyper link, how can I look at the 2nd and 3rd levels of the directory.

You also mentioned SO as a resource do you have a link?

Thanks for your valued help



After a lot of searching! Here it is: http://stackoverflow.com/questions/1688564/php-directory-list-from-remote-server

From about a year ago - should still be relevant. From the looks of it, I didn't tinker with it too much, used it pretty much "as is".

For subsequent directories, e.g. a /css/ directory may have an images/ subdirectory - you could check for the '/' at the end and feed that back into the mix as a recursive function (retTxt).

I'm sure you saw the deliberate(!) typo on line 14 - should be retTxt not retText!

Edited by diafol

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.