947740 Posted July 1, 2008 Share Posted July 1, 2008 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()? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 1, 2008 Share Posted July 1, 2008 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
947740 Posted July 1, 2008 Author Share Posted July 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 1, 2008 Share Posted July 1, 2008 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>"; } } ?> Quote Link to comment Share on other sites More sharing options...
947740 Posted July 1, 2008 Author Share Posted July 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 1, 2008 Share Posted July 1, 2008 You won't be able to read a remote directory if it has a default page (or if indexes are disabled)...nor should you be able to Quote Link to comment Share on other sites More sharing options...
947740 Posted July 1, 2008 Author Share Posted July 1, 2008 Well, thank you for your help. I promise, I was not trying to do anything...sinister...;-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.