Search code examples
phpopendir

I cant open a dir using php opendir


I have code as below:

$dir = opendir("D:/Marcin");
if ($dir) {
echo "OK";
}
else {
echo "not ok";
}

and I get not ok, why??


Solution

  • I believe you are trying to open a directory server-side. If you were doing so locally, it would work - as I can see that you are getting the directory from your PC, but instead, you are attempting to execute the script in the client-side, and getting a server-side result.

    This means that if that directory doesn't exist on your server, the server will not be able to find the file - and will return false.

    At a certain point, you must check whether the directory exists on your server.

    localserver === local directory = true;
    webserver === web directory = true;
    

    If both of the two are cross-referenced, it will return to false.

    $dir = opendir("D:/Marcin"); // Check whether the directory exists on your server.
    if ($dir) {
        echo "OK";
    } else {
        echo "Not OK";
    }
    

    Hope this assists.