Jump to content

using scandir() on other websites


947740

Recommended Posts

I want to read a directory from another website.  E.G. http://www.somewhere.com.  I then want to display a list of files from that directory.  The PHP manual says I have to use fopen(), but I am not sure how to use that in conjunction with scandir().  Basically, how do I read the directory using scandir() and fopen()?

Link to comment
https://forums.phpfreaks.com/topic/112764-using-scandir-on-other-websites/
Share on other sites

First, the remote directory MUST have directory listing turned on. aka, when you navigate with your browser to the directory, it shows the list of files there.

 

If the normal scandir() doesn't work:

<?php
$dir = "http://www.somewhere.com/";
foreach(scandir($dir) as $file){
  print '<a href="'.$dir.$file.'">'.$file.'</a><br>';
}
?>

try opendir():

<?php
$dir = "http://www.somewhere.com/";
$dh  = opendir($dir);
while (false !== ($file = readdir($dh))) {
  print '<a href="'.$dir.$file.'">'.$file.'</a><br>';
}
?>

Getting:

 

Warning: scandir(http://www.fmtabor.k12.ia.us/Teachers/shepherd/BoardPolicy/) [function.scandir]: failed to open dir: not implemented in D:\FM\test_read.php on line 3

 

Warning: scandir() [function.scandir]: (errno 2): No such file or directory in D:\FM\test_read.php on line 3

 

Warning: Invalid argument supplied for foreach() in D:\FM\test_read.php on line 3

 

--------------------------------------------------------------------------------

 

Warning: opendir(http://www.fmtabor.k12.ia.us/Teachers/shepherd/BoardPolicy/) [function.opendir]: failed to open dir: not implemented in D:\FM\test_read.php on line 10

 

Warning: readdir(): supplied argument is not a valid Directory resource in D:\FM\test_read.php on line 11

yeah...what you will have to do, is read the page like a webpage and parse it for the info. not sure what info you want or what you are trying to do with it...but this should get you headed in the right direction:

 

<?php
$html = file_get_contents("http://www.fmtabor.k12.ia.us/Teachers/shepherd/BoardPolicy/");
if(preg_match_all('/<br>\s+(.+?)\s+<(\w+)>\s+<A HREF="(.+?)">(.+?)<\/A>/',$html,$matches)){
  foreach(array_keys($matches[0]) as $n){
    $date = $matches[1][$n];
    $type = $matches[2][$n];
    $url = $matches[3][$n];
    $name = $matches[4][$n];
    print "<hr><br>Date: $date<br>Type: $type<br>Url: $url<br>Filename: $name<br>";
  }
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.