Count how many files in directory PHP
You can simply do the following :
$fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
printf("There were %d Files", iterator_count($fi));
PHP Count how many files in directory
You have forgotten to escape your first backslash. Use this:
$directory = 'api\\config\\';
One thing that will help for debugging in the future, is to try and output (echo
) your $directory, to make sure the directory value is being passed to your functions correctly.
Count number of files in folder in php
Glob returns an array
, on error it returns false
.
Try this:
$directory = '/var/www/ajaxform/';
$files = glob($directory . '*.jpg');
if ( $files !== false )
{
$filecount = count( $files );
echo $filecount;
}
else
{
echo 0;
}
How to count number of files in a directory using PHP?
non-recrusive:
$dir = opendir('dir/');
$i = 0;
while (false !== ($file = readdir($dir))){
if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
}
echo "There were $i files";
recrusive:
function crawl($dir){
$dir = opendir($dir);
$i = 0;
while (false !== ($file = readdir($dir)){
if (is_dir($file) and !in_array($file, array('.', '..'))){
$i += crawl($file);
}else{
$i++;
}
}
return $i;
}
$i = crawl('dir/');
echo "There were $i files";
How to count files inside a folder using PHP?
I'm doing it with DirectoryIterator
$files_in_directory = new DirectoryIterator($path_to_folder);
$c = 0;
foreach($files_in_directory as $file)
{
// We want only files
if($file->isDot()) continue;
if($file->isDir()) continue;
$c++;
}
var_dump($c);
For use as function :
function folderlist($directories = array(), $extensions = array())
{
if(empty($directories))
return false;
$result = array();
$total_count = 0;
foreach($directories as $directory)
{
$files_in_directory = new DirectoryIterator($directory);
$c = 0;
foreach($files_in_directory as $file)
{
// We want only files
if($file->isDot()) continue;
if($file->isDir()) continue;
// This is for php < 5.3.6
$file_extension = pathinfo($file->getFilename(), PATHINFO_EXTENSION);
// If you have php >= 5.3.6 you can use following instead
// $file_extension = $fileinfo->getExtension()
if(in_array($file_extension, $extensions)){
$c++;
$result['directories'][$directory]['files'][$c]['name'] = $file->getFilename();
$result['directories'][$directory]['files'][$c]['path'] = $file->getPath();
$result['directories'][$directory]['count'] = $c;
}
}
$total_count += $c;
}
$result['total_count'] = $total_count;
return $result;
}
Displaying results based on GET superglobal:
if(isset($_GET['album']) && !empty($_GET['album']) && !isset($_GET['listfiles']))
{
// We are in directory view mode
$album = $_GET['album'];
// View all directories and their file count for specified album
$view_directory = folderlist(array($album), array('jpeg', 'jpg', 'log'));
// Loop the folders and display them with file count
foreach ($view_directory['directories'] as $folder_name => $folder_files){
$count = $folder_files['count'];
echo '<li>';
echo '<a href="files.php?album=' . $folder_name . '&listfiles=1" class="style1">';
echo '<strong>' . basename($folder_name) . '</strong>';
echo ' (' . $count . ' files found)';
echo '</a>';
echo '</li>';
}
echo "Total Files:". $get_dir_info['total_count'];
}
elseif(isset($_GET['album'], $_GET['listfiles']) && !empty($_GET['album']))
{
// We are in file view mode for folder
$album = $_GET['album'];
// View all files in directory
$view_files = folderlist(array($album), array('jpeg', 'jpg', 'log'));
echo 'Showing folder content of: <b>'.basename($album).'</b>';
foreach($view_files['directories'][$album]['files'] as $file)
{
$path = $file['path'];
$name = $file['name'];
echo '<li>';
echo '<a href="files.php?file=' . $name . '&path=' . $path . '" class="style1">';
echo '<strong>' . $name . '</strong>';
echo '</a>';
echo '</li>';
}
}
php count files from multiple folders and echo total
Yes you can:
glob('../{health,food,sport}/*.php', GLOB_BRACE);
Related Topics
PHP Explode the String, But Treat Words in Quotes as a Single Word
Causes of MySQL Error 2014 Cannot Execute Queries While Other Unbuffered Queries Are Active
Posting Form Fields With Same Name Attribute
How to Install MySQLi on Macos
Multiple Files Upload in Codeigniter
Send Value of Submit Button When Form Gets Posted
Finding the Subsets of an Array in PHP
MySQLi_Query() Expects Parameter 1 to Be MySQLi, Object Given
How to Iterate Over the Results in a MySQLi Result Set
Fastest Way to Serve a File Using PHP
How Are Echo and Print Different in PHP
How to Show Multiple Recaptchas on a Single Page
PHP: Storing 'Objects' Inside the $_Session
Converting a Number With Comma as Decimal Point to Float
How to Fake $_Server['Remote_Addr'] Variable