Error: readdir() expects Parameter 1 to be Resource, Boolean Given

Asked

Viewed 393 times

1

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

readdir() expects Parameter 1 to be Resource, Boolean Given in

When there is no folder.

1 answer

3


Following the PHP manual, the code can be 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 excerpt: $dh = opendir($dir) find nothing returns FALSE.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.