Jump to content

opendir for URL's


fanfavorite

Recommended Posts

Is there a way to specify a directory by http url to read and get the contents of that directory if directory listings are set?  As far as I know you can't do this with opendir, I get:  "failed to open dir: not implemented"  From what I understand in the PHP Manual, you can't use http, so is there any other function or a work around to do something like this?  Here is my code using opendir.     

 

for ($id = 1; $id < 5867; $id++) {

$dir = "http://somedomain.com/folder/".$id;

 

$dirHandle = opendir($dir);

  while ($file = readdir($dirHandle)) {

if ($file != ".." AND $file != ".") {

$img = $dir."/".$file;

$data = file_get_contents($img);

$file2 = fopen($copydir . $id . "/".$file, "w+");

fputs($file2, $data);

fclose($file);

}

  }

}

Link to comment
https://forums.phpfreaks.com/topic/105071-opendir-for-urls/
Share on other sites

Actually it is possible if it is set to allow directory listings.  Something like:

 

for ($id = 1; $id < 5867; $id++) {

$dir = "http://somedomain.com/folder/".$id."/";

$copydir = "/path/to/dir/".$id."/";

 

$f = fopen($dir, "rb");

$listing = '';

 

while (!feof($f)){

$listing .= fread($f, 8192);

}

fclose($f);

 

preg_match("(<a[^<]+)(?=<\/a>)(<\/a>)", $listing, $listing2);

for ($i = 0; $i < count($listing2); $i++) {

$listing2[$i] = eregi_replace ("<a[^>]*>","",$listing2[$i]);

$listing2[$i] = str_replace ("</a>","",$listing2[$i]);

}

 

  foreach ($listing2 as $file) {

if ($file != "Parent Directory") {

$img = $dir."/".$file;

$data = file_get_contents($img);

$file2 = fopen($copydir . $file, "w+");

fputs($file2, $data);

fclose($file);

}

  }

}

 

Now I need to fix up the preg_match, but it's almost there. 

Link to comment
https://forums.phpfreaks.com/topic/105071-opendir-for-urls/#findComment-538514
Share on other sites

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.