Answer the question
In order to leave comments, you need to log in
Script that counts the number of items in a folder?
I have a script that counts the number of items in a folder, the script looks like this:
<?php
// integer starts at 0 before counting
$i = 0;
$dir = '50/images/';
if ($handle = opendir($dir)) {
while (($file = readdir($handle)) !== false){
if (!in_array($file, array('.', '..')) && !is_dir($dir.$file))
$i++;
}
}
// prints out how many were in the directory
echo "$i items";
?>
Answer the question
In order to leave comments, you need to log in
Don't do it like that, from the word never.
To avoid such garbage:
1. First check if the path is a directory.
2. Then do the actions.
When working with paths, it's best to use global paths.
$path = __DIR__ . '/50/images/';
if (!is_dir($path)) {
throw new \RuntimeException(sprintf('Incorrect path: %s', $path));
}
$foundFiles = array_diff(scandir($path), ['..', '.']);
// Warning, it could be folders to
echo count($foundFiles);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question