Num files in php

How to count number of files in a directory using PHP?

Solution 3: First, use glob () to get file list array, then use count () to get array length, the array length is file count. Thus, its use can further be extended to counting files with specific filenames, specific extensions or both.

How to count number of files in a directory using PHP?

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); $count = 0; while($it->next()) $count++; 

Most of the mentioned ways for «Non-Recursive Search» work, though it can be shortened using PHP’s glob filesystem function.

It basically finds pathnames matching a pattern and thus can be used as:

$count = 0; foreach (glob('path\to\dir\*.*') as $file)

The asterisk before the dot denotes the filename, and the one after denotes the file extension. Thus, its use can further be extended to counting files with specific filenames, specific extensions or both.

$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"; 
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"; 

Count number of files in folder in php, if (glob ($directory . «*.jpg») != false) $filecount = count (glob ($directory . «*.jpg»)); else $filecount = 0; Share Improve this answer answered Jan 7, 2013 at 10:43 Suresh Kamrushi 14.7k 12 74 87 Add a comment

Читайте также:  Php sql admin login

How to count number of files in folder in php?

Use the FilesystemIterator as shown:

$dir = "/path/to/folder"; $fi = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS); $fileCount = iterator_count($fi); 

Nothing easier: use opendir() and readdir() just like follow:

Obviously this doesn’t take into account file extensions.

  • scandir() —> read here
  • FilesystemIterator class (as dops’s answer correctly suggest) —> read here

You could just let PHP find the files for you. then count them.

$count = count(glob("$path_to_user_dir/*")); 

Php — How to find fast the number of files in directory, Mostly I need to know just number of these files in directory. I fear that this attitude: $files = count (scandir («logs»)) — 2; Is not very effective (memory, filesystem). I am considering some global counter (incremented after file is added), unless there is some other effective way? php Share asked Jun 2, 2011 at 7:27 …

How to count number of .txt files in a directory in PHP?

Use php DirectoryIterator or FileSystemIterator:

$directory = new DirectoryIterator(__DIR__); $num = 0; foreach ($directory as $fileinfo) < if ($fileinfo->isFile()) < if($fileinfo->getExtension() == 'txt') $num++; > > 

Take a look at the glob() function: http://php.net/manual/de/function.glob.php
You can then use sizeof() to count how many files there are.

First, use glob () to get file list array, then use count () to get array length, the array length is file count.

$txtFileCount = count( glob('/moviefiles/moviename/review*.txt') ); 

Php — Efficiently counting the number of lines of a text, Using a loop of fgets() calls is fine solution and the most straightforward to write, however:. even though internally the file is read using a buffer of 8192 bytes, your code still has to call that function for each line.

Return total number of files within a folder using PHP

Check out the Standard PHP Library (aka SPL) for DirectoryIterator:

$dir = new DirectoryIterator('/path/to/dir'); foreach($dir as $file )

(FYI there is an undocumented function called iterator_count() but probably best not to rely on it for now I would imagine. And you’d need to filter out unseen stuff like . and .. anyway.)

This will give you the count of what is in your dir. I’ll leave the part about counting only images to you as I am about to fallll aaasssllleeelppppppzzzzzzzzzzzzz.

iterator_count(new DirectoryIterator('path/to/dir/')); 
$files = scandir($dir); $x = count($files); echo $x; 

but it also counts the . and ..

Php count number of files in directory Code Example, $fi = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS); printf(«There were %d Files», iterator_count($fi));

Источник

How to Count Number of Files in Directory using PHP?

How to Count Number of Files in Directory using PHP?

As a PHP developer, you may often need to work with files and directories. One common task is to count the number of files in a directory.

In this blog post, we’ll explore how to use PHP to count the number of files in a directory.

How to Count Number of Files in Directory using PHP?

To count the number of files in a directory, we can use the built-in PHP function scandir() . The scandir() function returns an array of files and directories from the specified directory. We can then count the number of files in the array using the count() function. Here’s an example of how to use it:

$dir_path = '/path/to/my/directory'; $files = scandir($dir_path); $num_files = count($files) - 2; // subtract 2 to exclude '.' and '..' echo "The directory $dir_path contains $num_files files.";

In this example, we first define the directory path in a variable $dir_path. We then use the scandir() function to get an array of files and directories from the specified directory. We subtract 2 from the count of the files array to exclude the “.” and “..” directories that are returned by scandir() . Finally, we print the number of files in the directory to the screen using the echo statement.

It’s important to note that the count() function will also count subdirectories if they exist in the directory. If you only want to count files in the top-level directory, you can use a loop to iterate through the files array and count only the files. Here’s an example:

$dir_path = '/path/to/my/directory'; $files = scandir($dir_path); $num_files = 0; foreach ($files as $file) < if (is_file($dir_path . '/' . $file)) < $num_files++; >> echo "The directory $dir_path contains $num_files files.";

In this example, we use a foreach loop to iterate through the files array. We check whether each file is a regular file using the is_file() function. If the file is a regular file, we increment the $num_files variable. Finally, we print the number of files in the directory to the screen using the echo statement.

Conclusion:

Counting the number of files in a directory is a common task in PHP development. Fortunately, PHP provides a built-in function, scandir() , that can help us achieve this easily. By using the examples above, you should now be able to count the number of files in any directory in your PHP code. If you have any questions or comments, please leave them below.

If you found this blog post helpful, please consider sharing it on social media or leaving a comment below. Also, feel free to check out our other PHP tutorials and resources on our website.

Источник

Count Files in a Directory in PHP

Count Files in a Directory in PHP

  1. Use the FilesystemIterator Class to Count the Files in a Directory in PHP
  2. Use the glob() and count() Functions to Count the Files in a Directory in PHP
  3. Use the scandir() and count() Functions to Count the Files in a Directory in PHP

This article will introduce and demonstrate a few methods to count the files in a directory in PHP.

Use the FilesystemIterator Class to Count the Files in a Directory in PHP

PHP provides the FilesystemIterator class, an elegant way of counting the files in a directory. The class contains a lot of functions and constants that perform various operations for iterating a filesystem .

The use of the iterator_count() function counts the number of elements in the iterator.

Let’s consider the following directory structure.

myDir ├── abc.html ├── abc.jpeg ├── abc.php └── abc.png 

Firstly, create a variable $iterator and store an instance of the FilesystemIterator() . Set the first parameter as __DIR__ and use FilesystemIterator::SKIP_DOTS as the second parameter in the constructor.

The __DIR__ represents the current directory where the script is located, and the second parameter is a flag that skips . and .. that exist in a directory. The $iterator variable contains the filesystem iterator for the current directory.

It can be iterated to get information like filenames, number of files, etc.

Next, use the iterator_count() function to print the number of $iterator in the directory.

$iterator = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS); echo "There are ". iterator_count($iterator). " files in the directory"; 
There are 4 files in the directory 

Therefore, we can use the FilesystemIterator() class to count the files in a directory.

Use the glob() and count() Functions to Count the Files in a Directory in PHP

We can use the glob() and count() functions to get the number of files in a directory. The glob() function gives an array of the files and the folders that match the specified pattern.

We can get all the files inside the directory using * . Next, we can use the count() function to get the number of elements in that array.

For example, create a $path variable and store the exact path of a directory whose files are to be counted. Next, use the glob() function to the $path variable and concatenate the variable with /* .

As a result, an array of all the files inside the myDir is created. Finally, use the count() function on the result of the glob() function.

Thus, the number of files inside the myDir directory can be found. The code example below represents the file structure used in the first example.

$path =$_SERVER['DOCUMENT_ROOT']."/myDir"; $num = count(glob($path . "/*")); echo $num; 

Use the scandir() and count() Functions to Count the Files in a Directory in PHP

This method is a similar approach to the second method. The only difference here is we use the scandir() function instead of the glob() function.

We can supply the directory path as the parameter to the scandir() function. The function returns an array with all the contents inside the directory, including the . and .. .

Next, we can use the count() function to get the number of the contents inside the directory. Finally, the important part is to subtract 2 from the result of the count() function to remove the count of . and .. .

Thus, we can get the exact number of files inside a directory.

$path =$_SERVER['DOCUMENT_ROOT']."/myDir"; $num = count(scandir($path))-2; echo "there are $num files in the directory"; 
there are 4 files in the directory 

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

Related Article — PHP Directory

Источник

Оцените статью