Count files in a directory using php

Published


If you want to count how many files are in there in a folder using php, you can use this script.
Copy This script, Change DIRECTORYNAME to the directory you want to count files, then save it as a .php file, upload to your server.

<?php
$directory = "DIRECTORYNAME";
if (glob($directory . "*.*") != false)
{
 $filecount = count(glob($directory . "*.*"));
 echo $filecount;
}
else
{
 echo 0;
}
?>

Code Explained



<?php
$directory = "DIRECTORYNAME";  //Saving directory path to variable $directory.
if (glob($directory . "*.*") != false) //checks whether there is ANY files inside. if yes-
{
 $filecount = count(glob($directory . "*.*")); counting the files using "count" function.and save result
                    to  $filecount
 echo $filecount; //displays the result .
}
else //if there was no files,
{
 echo 0; //displaying a 0.
}
?>


Function used

count();

count fuction is use to count the files.

Comments

Post a Comment