Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read the directory contents on a remote server with PHP?

Tags:

directory

php

I have a url, http://www.mysite.com/images and the images directory allows Directory Listings. How can I get the files in that directory with PHP?

like image 487
Shamoon Avatar asked Sep 15 '11 21:09

Shamoon


People also ask

How can I get directory file in PHP?

The scandir() function returns an array of files and directories of the specified directory.

How can access file from another folder in PHP?

To access the root begin your path with "/". If you want to go up one directory from where you are currently, e.g. from /x/a/ to /x/ you could use "../". If you want to go back up two directories (e.g. from /x/a/ to /) you could use "../../" (rather than ".../" which you mentioned).

How directories are used in PHP?

A new directory can be created in PHP using the mkdir() function. This function takes a path to the directory to be created. To create a directory in the same directory as your PHP script simply provide the directory name. To create a new directory in a different directory specify the full path when calling mkdir().

What is remote control PHP?

"Remote Control" is a PHP class library that allows you to programically control a remote device via its CLI interface (usually via SSH or Telnet) or any other command via STDIN and STDOUT using Expect in an easy to use object oriented manner.


3 Answers

Here is an example if you need to read the images over HTTP and the server is Apache:

<?php
$url = 'http://www.mysite.com/images';

$html = file_get_contents($url);

$count = preg_match_all('/<td><a href="([^"]+)">[^<]*<\/a><\/td>/i', $html, $files);

for ($i = 0; $i < $count; ++$i) {
    echo "File: " . $files[1][$i] . "<br />\n";
}

?>

If it is the same server you are running your PHP on, you can use opendir() and readdir().

like image 166
drew010 Avatar answered Nov 05 '22 10:11

drew010


I know this question is very old, but just to get me into the swing of using this forum I thought I'd add my view. I found the following happened (referring to the original answer to use regex.

My html turned out to be formatted like this:

<td>
<a href="bricks.php">bricks.php</a>
</td>

So I ended up using this:

$count = preg_match_all('/<a href=\"([^\"?\/]+)">[^<]*<\/a>/i', $html, $files);

I wanted to use the following (which tested ok in the online generator testers, but it failed to find a match in the php code):

$count = preg_match_all('/<td>(?:[\w\n\f])<a href="([^"]+)">[^<]*<\/a>(?:[\w\n\f])<\/td>/i', $html, $files);
like image 41
andym2009 Avatar answered Nov 05 '22 12:11

andym2009


You need FTP access (an FTP account to that URL). If you have this, then you can log into the server with FTP and use:

opendir()

and

readdir()

to accomplish what you are trying to do.


If you do not have access to the server, you will need to scrape the site's HTML, and it gets more complex -> so I can let somebody else tackle that one... but google search "scrape html site" or something similar, there are plenty of pre-written functions that can do similar things.

i.e. http://www.thefutureoftheweb.com/blog/web-scrape-with-php-tutorial

http://www.bradino.com/php/screen-scraping/

like image 1
Shackrock Avatar answered Nov 05 '22 10:11

Shackrock