Error: readdir () expects parameter 1 to be resource, boolean given

1

How do I return an empty value in the opendir($dir); command in php? I would like that when there is no folder, return with the value zero because it is giving error:

  

readdir () expects parameter 1 to be resource, boolean given in

When there is no folder.

    
asked by anonymous 07.08.2017 / 17:22

1 answer

3

Following the PHP manual , the code looks like this:

<?php

 $dir = "/etc/php5/"; 

 // Abre um diretorio conhecido, e faz a leitura de seu conteudo
 if (is_dir($dir)) {
    if ($dh = opendir($dir)) { // <-- AQUI VALIDA  SE RETORNA ALGO
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
 }
?>

If the section: $dh = opendir($dir) does not find anything returns FALSE .

    
07.08.2017 / 17:25