Search code examples
phpglob

PHP Count how many files in directory


I was looking for some answers here but non of them worked for me what I have right now is

$directory = 'api\config\\';
$filecount = 0;
$files = glob($directory . '*.ini');

if ( $files !== false )
{
$filecount = count( $files );
echo $filecount;
}

The problem is $filecount is always 0

I'm using php 5.6 (required by the facility I'm working with). the directory I want to use (..\api\config).

any ideas why is this not working with me?


Solution

  • 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.