-2

I have in folder "files" some folders. I want to return the count of this folders by my php code:

$x = count(scandir('/files'));
echo $x;

But this is not working. What is wrong?

Bista
  • 7,869
  • 3
  • 27
  • 55
Artimal
  • 651
  • 7
  • 24

3 Answers3

2

If you have some files in files folder, Here is the solution.

  $directory = 'files/';
  $scanned_directory = array_diff(scandir($directory), array('..', '.'));//to remove dots
  $x = count($scanned_directory);
  echo $x;

Try not to use /files. It will look for the file named files

A J
  • 3,970
  • 14
  • 38
  • 53
1

Try this:

$directory = '/your/directory/path/';
$files = glob($directory . '*.*');   // returns an array on success and false on error.

if ( $files !== false )
{
    $filecount = count( $files );
    echo $filecount;
}
else
{
    echo 0;
}
Jayesh Patel
  • 289
  • 3
  • 12
1

its better to check if directory does even exist first :

$directory = '/your/directory/path/';
if(!is_dir($directory))
    die("direction not exists");

ant then count and remove . and .. elements

Mojtaba Hn
  • 457
  • 2
  • 10