Laravel File Listing & Counting by Filtered File Names

- 1 answer

Ad

I want to count the number of files I filter by name. I wrote the following code:

<?php
    $directory = Config::get('app.app_working_directory')."/".$form_constant["dir"];
    foreach(File::allFiles($directory) as $file)
    {
        if (starts_with($file->getFilename(), $file_tag."-".$id)) {
?>
                        {{HTML::image($form_constant["dir"].'/'.$file->getFilename(), $id, array('class' => 'col-md-3 img-thumbnail img-responsive'))}}
<?php
        }
    }
?>

But this is just for listing files on directory. My stored files like this:

machine-parts-103-09122015150511.jpg
machine-parts-103-09122015150515.jpg
machine-parts-1072-09122015143157.jpg

Format: machine-part-$id-microtime(). How I can count like a just filename starting "machine-part-$id-"? Example for this file names: "Total File:2" for "machine-parts-103-". Thanks...

Ad

Answer

Ad

You could use the glob function to count the number of files that matches a wildcard like this:

glob("/path/to/directory/machine-parts-103-*.jpg");

This will return you an array with all the files names that matches that pattern and you can count the number of items in the array.

Ad
source: stackoverflow.com
Ad