Jump to content

Use readdir() over ftp


Yesideez

Recommended Posts

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:[email protected])

 

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?

Link to comment
https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268544
Share on other sites

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 :D

Link to comment
https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268565
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268596
Share on other sites

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://[email protected]) [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?

Link to comment
https://forums.phpfreaks.com/topic/54304-use-readdir-over-ftp/#findComment-268602
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.