3

I wanna check if there any image on a folder from my server. I have this little function in PHP but is not working and I don't know why:

$path = 'folder/'.$id;
function check($path) {
    if ($handle = opendir($path)) {
        $array = array();
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && count > 2) {
                echo "folder not empty";
            } else {
                echo "folder empty";
            }
        }
    }
    closedir($handle);
}

Any help will be appreciated, thanks in advance.

1
  • 1
    what is the count variable (I'm assuming)? Does it ever change? Commented Nov 15, 2010 at 19:04

4 Answers 4

5

It does not work because count is coming from nowhere. Try this instead:

$path = 'folder/'.$id;
function check($path) {
    $files = glob($path.'/*');
    echo empty($files) ? "$path is empty" : "$path is not empty";
}
Sign up to request clarification or add additional context in comments.

4 Comments

Vars out of quotes... Can debug much of your code ;) pfz.nl/wiki/variabelen-buiten-quotes (dutch)
Just try to translate it with google translate... It is a article that explains the (bad) behavior of php with: "$path is empty"
I wasn't asking about the article, I got that part. It's just your comment that doesn't make sense. Besides, there's nothing wrong with PHP's variable expansion and parsing... Perl does it, Bash does it, a lot of other do it as well. It's only wrong when you use it wrong.
I didn't say something is wrong... (I'm thinking to much in my native language.) My comment is on the "$path is empty" and "$path is not empty" part of your answer... If you build a whole system, using double quotes " and variables inside these quotes "$path" then your system will slow down when you have much refreshes... Use ' instead of " and $path.' is empty'. This can give your system a boost if it is big enough...
1

Try this function: http://www.php.net/glob

Comments

1

Try This:

$path = 'folder/'.$id;
function check($path) {
    if (is_dir($path)) {
        $contents = scandir($path);
        if(count($contents) > 2) {
            echo "folder not empty";
        } else {
            echo "folder empty";
        }
    }
    closedir($handle);
}

It counts the contents of the path. If there are more than two items, then its not empty. The two items we are ignoring are "." and "..".

Comments

1
Step 1: $query = select * from your_table where id=$id;
Step 2: $path=$query['path_column'];
Step 3: if($path!=null&&file_exit($path)&&$dir=opendir($path)){
           while (($file = readdir($dir )) !== false)
            {
                if ($file == '.' || $file == '..') 
                {
                    continue;
                }
                if($file) // file get 
                { 
                    $allowedExts = array("jpg");
                    $extension = pathinfo($file, PATHINFO_EXTENSION);
                    if(in_array($extension, $allowedExts))
                    $file[]=$file;
                }
                $data[file_name'] = $file;
            }
             closedir($dir);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.