Yesideez Posted June 5, 2007 Share Posted June 5, 2007 I'm playing with reading the contents of a directory and am wondering if this is possible: if ($ndlDirLock=opendir('ftp://USERNAME:PASSWORD@URL')) { Where USERNAME is my username, PASSWORD is my password and URL is the web URL to my website (eg. ftp://user:pass@www.myurl.com) If I enter the ftp string directly into my browser I can login OK but entering it into my code it doesn't work. Do I have to define "resource $context" in opendir() or is this not possible due to security? Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/ Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 opendir only works on local filesystems. Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268510 Share on other sites More sharing options...
Yesideez Posted June 5, 2007 Author Share Posted June 5, 2007 If that is the case why does the PHP manual refer to this? ChangeLog Version Description 5.0.0 path supports the ftp:// URL wrapper. 4.3.0 path can also be any URL which supports directory listing, however only the file:// URL wrapper supports this in PHP 4 That is mentioned on this page: http://www.php.net/manual/en/function.opendir.php -Very puzzled ... Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268544 Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 Sorry... my bad. Are url_wrappers enabled in your php.ini? One would assume that is required. Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268551 Share on other sites More sharing options...
Yesideez Posted June 5, 2007 Author Share Posted June 5, 2007 URL wrappers? Do I have to do something special to my URL before I can use it or can I specify it as plain text? Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268554 Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 Do I have to do something special to my URL No. Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268560 Share on other sites More sharing options...
Yesideez Posted June 5, 2007 Author Share Posted June 5, 2007 OK thanks for your help, I'll give it a go later by adding a folder with various permissions being set to allow access and see how that works. My script so far reads the contents of the folder and displaying folders in alphabetical order then files in alphabetical order. I've tested it using the root folder and it works that way - next for the ftp method Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268565 Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 Are url_wrappers enabled in your php.ini? Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268568 Share on other sites More sharing options...
Yesideez Posted June 5, 2007 Author Share Posted June 5, 2007 No idea - I've never accessed the php.ini file and have no idea where it's stored! Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268570 Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 What is the output of.... <?php echo ini_get("allow_url_fopen"); ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268582 Share on other sites More sharing options...
Yesideez Posted June 5, 2007 Author Share Posted June 5, 2007 I get the number 1. Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268591 Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 Well, url wrappers are enabled then. Still not sure if you actually need them in order for this to work, but its good to know. Sorry, but I'm not real sure why its not working. make sure you have error_reporting and display_errors tunred on... lets see if we can't produce an error at least. Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268596 Share on other sites More sharing options...
Yesideez Posted June 5, 2007 Author Share Posted June 5, 2007 This is the entire script: <?php $strDirName='ftp://www.pictureinthesky.net'; //$_SERVER['DOCUMENT_ROOT']; $arrContents=array('dir' => array(),'file' => array()); $strMessage=''; if ($ptrLock=opendir($strDirName)) { clearstatcache(); while (false!==($strFilename=readdir($ptrLock))) { if ($strFilename!='.'&&$strFilename!='..') { if (is_dir($strDirName.'/'.$strFilename)) { array_push($arrContents['dir'],$strFilename); } else { array_push($arrContents['file'],$strFilename); } } } sort($arrContents['dir']); sort($arrContents['file']); closedir($ptrLock); } else { $strMessage='ERROR: Unable to lock directory ('.$strDirName.')'; } ?> <html> <head> <title>Dir Test</title> <style type="text/css"> body {font: 10px verdana} </style> </head> <body> <?php if (empty($strMessage)) { echo 'Directory: '.$strDirName.'/<br /><br />'; for ($i=0;$i<count($arrContents['dir']);$i++) { echo $arrContents['dir'][$i].'/<br />'; } for ($i=0;$i<count($arrContents['file']);$i++) { echo $arrContents['file'][$i].'<br />'; } } else { echo $strMessage; } ?> </body> </html> This is the error I get: Warning: opendir(ftp://www.pictureinthesky.net) [function.opendir]: failed to open dir: not implemented in /home/zeb/public_html/testcode/index.php on line 5 ERROR: Unable to lock directory (ftp://www.pictureinthesky.net) If I add my username and password into the first line I get this: Warning: opendir(ftp://...@www.pictureinthesky.net) [function.opendir]: failed to open dir: not implemented in /home/zeb/public_html/testcode/index.php on line 5 Seems strange that I get "not implemented" in the error message yet it says in the documentation it is! btw, I changed to http rather than ftp as I'm using PHP version 4.4.4 - opendir needs version 4.3 - maybe directory listing isn't supported on my server? Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268602 Share on other sites More sharing options...
trq Posted June 5, 2007 Share Posted June 5, 2007 Sorry, but is this actually a remote server your trying to access? Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268606 Share on other sites More sharing options...
Yesideez Posted June 5, 2007 Author Share Posted June 5, 2007 Yes, it's where my web space is held. Going by the documentation for opendir() mentioning that ftp and http is supported I'm presuming that it's possible to do this? Quote Link to comment https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268608 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.