PHP Directory List from Remote Server

PHP directory list from remote server

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

?>

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

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().

Go through files in folder at another server PHP

I used the following method:

I created a script which goes through all the files at the file server.

$fileList = glob($dir."*.*");

This is only possible if the script is actually on the fileserver. It would be rather strange to go through files at another server without having access to it.

There is a way to do this without having access (read my other answer) but this is very slow and not coming in handy.

I know I said that I didn't have access, but I had. I just wanted to know all the possibilities.

get file list from remote dir and return it as an array

basically, this worked.. thanks all

in the html:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" 
function loadjquery () {
$.ajax({
"url": "ajaxV6.php",
"type": "GET",
"dataType": "json",
"success": function( data, status, xhr ){
alert("this is returned from the php ->>" + data[1] );
noaafilelist = ( data[1] ); //<< assign to global var

}
});

}

and..

function showResult (){
alert('this is global ->>' + noaafilelist );
}

and.. in the PHP..
.. at very end, print

print json_encode($files[1]);

and to make it global for the rest of the html file add:

var noaafilelist; // out side the function

I'm sure this will seem clunky after a few months.. but right now it's at least working.

Dennis

How to list files of a directory in an other server using ssh2

You can use ssh2_sftp and opendir, like this:

<?php
$connection = ssh2_connect('shell.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$sftp = ssh2_sftp($connection);
$sftp_fd = intval($sftp);

$handle = opendir("ssh2.sftp://$sftp_fd/path/to/directory");
echo "Directory handle: $handle\n";
echo "Entries:\n";
while (false != ($entry = readdir($handle))){
echo "$entry\n";
}

php get all files from a remote directory

Based loosely on the code you already tried (but was riddled with problems). This grabs the full contents of the URL $url, parses out the <img> src attributes, and then outputs them.

Because this particular web host uses <base href=""/> tag to reset the base part of all URLs on the page, I've added a $base variable which you should set to the contents of the base tag.

Additionally, it looks like this particular web host has some pretty smart anti-hotlinking in place, so not all images may be visible.

But! Give it a whirl, let me know if it does what you need it to, and any questions.

<?php

$url = 'http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/';
$base = 'http://www.webtoonlive.com/';

// Pull in the external HTML contents
$contents = file_get_contents( $url );

// Use Regular Expressions to match all <img src="???" />
preg_match_all( '/<img[^>]*src=[\"|\'](.*)[\"|\']/Ui', $contents, $out, PREG_PATTERN_ORDER);

foreach ( $out[1] as $k=>$v ){ // Step through all SRC's

// Prepend the URL with the $base URL (if needed)
if ( strpos( $v, 'http://' ) !== true ) $v = $base . $v;

// Output a link to the URL
echo '<a href="' . $v . '">' . $v . '</a><br/>';
}

Sample output:

http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/000.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/001.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/002.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/003.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/004.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/005.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/006.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/007.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/008.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/009.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/010.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/011.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/012.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/013.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/014.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/015.jpg
http://www.webtoonlive.com/webtoon/fantasy_world_survival/ch02/016.jpg


Related Topics



Leave a reply



Submit