fanfavorite Posted May 10, 2008 Share Posted May 10, 2008 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 More sharing options...
Fadion Posted May 10, 2008 Share Posted May 10, 2008 A file can be opened with fopen() or file_get_contents(), but a directory isnt possible as far as i know. It should be a great concern on security opening directories and reading all its files, including here very sensitive information. Link to comment https://forums.phpfreaks.com/topic/105071-opendir-for-urls/#findComment-537885 Share on other sites More sharing options...
fanfavorite Posted May 11, 2008 Author Share Posted May 11, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.