-2

Lets say I have a lot of files, some of those are in these paths:

root/fonts/folder1/font1.ttf
root/fonts/folder1/font2.ttf
root/fonts/folder2/font1.ttf
root/fonts/folder2/font2.ttf
root/scripts/file.php

Remember that there are also other types of files in those folders. How can my "/scripts/file.php" iterate through the "../fonts/" directory tree and store all the TrueType font (.ttf) files into an array? Could you show me an example?

  • look for glob() http://php.net/manual/en/function.glob.php – Satya Jun 18 '15 at 11:32
  • 1
    This looks more like a **specification than a question** relating to a specific issue. Sorry we are not here to do your work for you for free. Make some effort to solve it and then ask for help on a specific issue. – RiggsFolly Jun 18 '15 at 11:33
  • possible duplicate of [How to read a list of files from a folder using PHP?](http://stackoverflow.com/questions/720751/how-to-read-a-list-of-files-from-a-folder-using-php) – thomasb Jun 18 '15 at 11:41
  • @RiggsFolly Sorry to hear that.. Just because I make my question clear it become like a specification. – 5ervant - techintel.github.io Jun 18 '15 at 11:41
  • **No thats not the reason**. You show no attempt at developing any kind of solution for yourself. Your question is basically asking for someone else to code a solution to your question. **Thats a specification** and not an **on topic** question. – RiggsFolly Jun 18 '15 at 11:47
  • @RiggsFolly Actually, I will then find a good solution later after I review the answers about my specific problem below. – 5ervant - techintel.github.io Jun 18 '15 at 11:56
  • @RiggsFolly Can you contribute to edit my question to have a better approach because sometimes I'm linking [that answer](http://stackoverflow.com/a/30914277/4883372) when needed to help others like [this one](http://stackoverflow.com/a/31163419/4883372), so I would not get another down-vote? – 5ervant - techintel.github.io Jul 02 '15 at 13:00

2 Answers2

1

SPL's recursive iterators are particularly useful for this type of functionality:

abstract class FilesystemRegexFilter extends RecursiveRegexIterator {
    protected $regex;
    public function __construct(RecursiveIterator $it, $regex) {
        $this->regex = $regex;
        parent::__construct($it, $regex);
    }
}

class FilenameFilter extends FilesystemRegexFilter {
    // Filter files against the regex
    public function accept() {
        return ( ! $this->isFile() || preg_match($this->regex, $this->getFilename()));
    }
}

class DirnameFilter extends FilesystemRegexFilter {
    // Filter directories against the regex
    public function accept() {
        return ( ! $this->isDir() || preg_match($this->regex, $this->getFilename()));
    }
}

$directory = new RecursiveDirectoryIterator(realpath(__DIR__ . '../fonts'));
$filter = new DirnameFilter($directory, '/^(?!\.)/');
$filter = new FilenameFilter($filter, '/(?:ttf)$/i');

$myArray = [];
foreach(new RecursiveIteratorIterator($filter) as $file) {
    $myArray[] = $file;
}

Though not sure why you need to build an array, and don't simply work with the files inside your foreach loop

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Robust! *(Looks better than using [this solution](http://stackoverflow.com/a/720758/4883372))* Just a little query, when I `var_dump` the returned `$file` I'm seeing such `object(SplFileInfo)#280 (2) { ["pathName":"SplFileInfo":private]=> string(81) "C:\xampp\apps\wordpress\htdocs\wp-content\themes\theme-name\fonts\folder\font.ttf" ["fileName":"SplFileInfo":private]=> string(8) "font.ttf" }` is there a way to get the *"fileName"* of this object? I tried `$file['fileName']` but there's a _"Fatal error: Cannot use object of type SplFileInfo as array in..."_ Other than `basename()` just curious.. – 5ervant - techintel.github.io Jun 18 '15 at 13:57
  • You mean like calling the SPLFileInfo object's `getFilename()` or `getBasename()` method? - http://www.php.net/manual/en/splfileinfo.getbasename.php – Mark Baker Jun 18 '15 at 14:00
0

This may be not the most elegant solution, but I once did the following:

$all_files = scandir("path/to/your/dir");
$selected_files = array();

foreach($all_files as $file)
{
  $tmp = explode(".", $file);
  if($tmp[1] == "ttf") { 
    array_push($selected_files, $file);
  }
}

Now the selected files are stored in selected_files array.

EDIT: Of course, in this solution, your files' names can have only one dot.

mmalik
  • 185
  • 2
  • 11