PHP scandir() not working

I am trying to use the scandir() function in PHP to get a list of files in a directory. However, it’s not working. Is it perhaps blocked? I could see that being true because then it might be possible to get a list of files in someone else’s account since this is a shared server.

Here is the code I’m using. I have tried several different directories, even starting from /home/vol3_4/ I’ve also tried different methods for printing the list, namely print_r, echo, and print. When I set the directory to /tmp (which I’m not sure what that is), it does give me a list, but it doesn’t work for any other directory. Is there an issue with my understanding of the code or is this a server limitation?


$path = '/DIRECTORY';
$files = scandir($path);

$files = array_diff(scandir($path), array('.', '..'));



Try this code, I used it an it worked fine:

$path = '/DIRECTORY';
if ($handle = opendir($path)) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {

            echo "$entry\n";


I just tried it and couldn’t get it to work. It just produced a 500 error when I went to that page. I couldn’t even get the directory “/tmp” to work, which previously worked with my code. What directory are you using? Can I simply use /htdocs or do I need to do /home/vol3_4/

I think the issue is with the directory name:

When you do this, it looks for a folder called DIRECTORY at the root folder of the entire server, not your hosting account. For security, your PHP scripts can’t access files outside of their website directories. We don’t want people poking around the server setting or other people’s websites, after all.

If you want to find the files in the folder DIRECTORY in your own website, you could do ./DIRECTORY to list the folder relative to the current directory.


That worked, thank you so much!!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.