Hi,

I am trying to open a directory using opendir() function. It is not opening any directory. My configuration is:

PHP: 4.3.3
Apache: 1.3.29
OS: Windows 2000 server

Here is my code:


$dir = "/img/";

if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
else echo "Not a valid Dir";


The above code gives me "Not a valid Dir" as output.

Thanks in advance. I really need this one asap.

    instead of
    $dir = "/img/";
    do
    $dir = $_SERVER['DOCUMENT_ROOT'] . "/img/"

    opendir uses absolute paths from the root of the server, not your webroot.

      actually, I think you need backslashes since it's a windows server.

      $dir = $_SERVER['DOCUMENT_ROOT'] . "\img\"

      Not sure about that because I've never worked with php/win. If you get an error, be sure to give it a shot.

        Cool. It works even with forward slashes. Thanks drew010 & mhalloran.

          14 years later

          Sitting with the same problem. Windows server, C:\inetpub\wwwroot\photos exists. Using
          $images_dir = $_SERVER['DOCUMENT_ROOT'].'\photos';
          $files = array();
          echo "trying to get files<br>";
          if($handle = opendir($images_dir))
          {
          while(false !== ($file = readdir($handle)))
          {
          $extension = strtolower(get_file_extension($file));
          if($extension && in_array($extension,$exts)) {
          $files[] = $file;
          }
          }
          closedir($handle);
          }
          else
          {
          echo "cannot opendir : ".$images_dir." , which is weird";
          }

          And this only displays:
          cannot opendir : C:\inetpub\wwwroot\photos , which is weird

          So I am stumped. What am I doing wrong? Are there permissions I need to set?

          Willeboer

          Was a case of permissions in the php.ini file. Ah well, live and learn! :-)

            Write a Reply...