10

How can i use opendir function to read the directory list of a remote server knowing its IP address?

Toby Allen
  • 10,997
  • 11
  • 73
  • 124
mck89
  • 18,918
  • 16
  • 89
  • 106

3 Answers3

14

Assuming you are trying to access the remote server using HTTP, you won't be able to do this directly. Since HTTP is really going access the web service of your remote server, it controls how the files are presented to you. In the case of many web servers, you can turn on "indexes" for folder contents. This causes the entries in the folder to be displayed in your browser as HTML. In order to traverse the directory structure, you would need to parse this HTML to find the path information.

If you are using FTP, you can pass the ftp://... URL to opendir() as of version 5.0. Note that this capability can be turned off by your server admin. If you cannot use this feature directly, see the FTP functions manual for PHP (including ftp_nlist() for listing files).

An example from the above references:

<?php

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");

// output $contents
var_dump($contents);

?>
jheddings
  • 26,717
  • 8
  • 52
  • 65
8

For files/folders that are visible to the public you can do it with this script: No FTP, no strugglin. It will give you back all filenames in a remote folder. If you can open the folder with a browser, you can also read it with php. Happy leeching ;)

UPDATE: This only lists the files that are available for the public, for sure not files that are only visible for a certain user!

function get_text($filename) {

    $fp_load = fopen("$filename", "rb");

    if ( $fp_load ) {

            while ( !feof($fp_load) ) {
                $content .= fgets($fp_load, 8192);
            }

            fclose($fp_load);

            return $content;

    }
}


$matches = array();

preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text('http://www.xxxxx.com/my/cool/remote/dir'), $matches);

foreach($matches[2] as $match) {
    echo $match . '<br>';
}
Sliq
  • 15,937
  • 27
  • 110
  • 143
  • It is only giving what the server is showing, which is a partial directory listing. Basically collecting all paths from links. Not listing. – Yekhezkel Yovel Jun 01 '14 at 11:19
  • 1
    No problem, but it's not directory listing. It doesn't show any index.html files for example. Or any other file that doesn't show in links. If all you need to do is just collect all anchor tags from the page it's fine. But if you need directory listing it is definitely not helping you. – Yekhezkel Yovel Jun 04 '14 at 01:26
  • Actually, it will return whatever that URL serves, which could be a web page. – Cuse70 May 31 '19 at 00:55
  • Is there anything against using PHPs function [file_get_contents](https://www.php.net/manual/en/function.file-get-contents.php) instead of your own function `get_text`? – Gero Dec 13 '19 at 09:47
  • The answer to @Gero 's question [Difference between file, file_get_contents, and fopen in PHP](https://stackoverflow.com/questions/24007898/difference-between-file-file-get-contents-and-fopen-in-php) – leopold Oct 21 '22 at 01:04
4

Assuming you have the ftp extension loaded for php on your server, you should be able to use phps ftp functions

Toby Allen
  • 10,997
  • 11
  • 73
  • 124