Jump to content

using scandir() on other websites


947740

Recommended Posts

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>';
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>";
  }
}
?>

Link to comment
Share on other sites

The only problem with that is, I want to read directories from websites that even have default files.  If there is a default file, I will not be able to read the information?

 

(Your code worked, in case you were wondering. ;-)

 

Thanks for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.