Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of files in folder in php

Tags:

php

<?php 
$directory = '/var/www/ajaxform/';
if (glob($directory . '.jpg') != false)
{
    $filecount = count(glob($directory . '*.jpg'));
    echo $filecount;
}
else
{
    echo 0;
}
?>

there are four jpg images in this directory but it returns 0

like image 432
Rohit Goel Avatar asked Jan 07 '13 10:01

Rohit Goel


People also ask

How do I count the number of files in a folder?

Alternatively, select the folder and press the Alt + Enter keys on your keyboard. When the Properties window opens, Windows 10 automatically starts counting the files and folders inside the selected directory. You can see the number of files and folders displayed in the Contains field.

How do I get a list of files in a directory in PHP?

you can esay and simply get list of file in folder in php. The scandir() function in PHP is an inbuilt function which is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories which are present inside a specified path.

Is PHP a directory?

The is_dir() function in PHP used to check whether the specified file is a directory or not. The name of the file is sent as a parameter to the is_dir() function and it returns True if the file is a directory else it returns False. Parameters Used: The is_dir() function in PHP accepts only one parameter.


1 Answers

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;
}
like image 165
Michel Feldheim Avatar answered Oct 11 '22 20:10

Michel Feldheim