0

I have folder, his name is "1", in this folder is 2 folders: "big_images" and "small_images"

I want delete folder "1" and his content, this is code:

function removeFullDir ($dir) {
$content = glob($dir."/*");

foreach ($content as $file) {
    if (is_dir($file)) {
        removeFullDir ($file);
    }
    else {
        unlink($file);
    }
}
    rmdir($dir);
}

removeFullDir("1");

in localhost (XAMPP server, php version 5.4.4) this code works good. in remote server this code also works, but returns warning:

Warning: Invalid argument supplied for foreach()

in remote server, php version is 5.2.42

please tell me, why obtain I this warning?

4

2 回答 2

1

This happens because glob does not return an array, which means that based on the documentation it must return false. This can happen when there is an error, but it can also happen when the directory happens to be empty:

Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.

Note: On some systems it is impossible to distinguish between empty match and an error.

To prevent the notice just guard against it:

function removeFullDir ($dir) {
    $content = glob($dir."/*");
    if(!$content) {
        return;
    }

    // ...
}
于 2012-10-12T09:19:15.160 回答
1

一个可能的原因可能是open_basedir限制,这意味着 glob() 返回 false 并且它将在 foreach 循环中出错,因为$content不是数组,因此您可以检查如下:

//before foreach
if(is_array($content) && count($content) > 0) {
于 2012-10-12T09:22:35.757 回答