0

I'm trying to get a list of all files and directories in a directory on CentOS 7 server by using a file management class. The directory I want to access is /monitor/demo3 (I checked).

The member function of the class designed to do this should return an array of all files and directories on success or false on failure. This is the part of the code where things stop working (The rest of the code isn't necessary to explain this issue):

try {
    $handle = opendir($path);

    if ($handle !== false) {
        /*
          Some code that's never executed 
          because $handle is always false.
        */
    } else {
        echo 'Failed to open directory! ' . $path;
        return false;
    }
} catch (Error $err) {
    echo $err->getMessage();
    return false;
}

So, according to official PHP documentation, opendir() returns a directory handle on success or throw an error and return false on failure. But in this case, it somehow returns false, but doesn't throw an error. This happens regardless to $path being a valid directory path or not.

I've tried making it work with scandir() instead, but the exact same thing happens.

When I run this on my Windows 10 PC, it works as intended and returns an array or throws an error. All I've changed was the directory path and solutions with both opendir() and scandir() worked.

User apache is in the wheel group with sudo rights, all the directories and files in question have 777 permissons (temporarily, for testing). I tried copying php.ini from my computer to the server. I'm completely out of ideas.

  • 1
    Welcome to the world of SELinux. Did you configure it? Before working with any Linux distribution you are supposed to know its basics and grasp the basics. – Mike Doe Jun 09 '20 at 09:10
  • According to the docs ("Returns a directory handle resource on success, or FALSE on failure") it (`opendir()`) returns FALSE on failure! – Luuk Jun 09 '20 at 09:11

0 Answers0