Posted by Marcelo Sabadini in
14/11/2008
CakePHP - Search / list files in a folder
Hello guys, this week I needed to find PNG files in a folder. How do I use CakePHP for this project, I used the lib Folder (click and see the class documentation).
It's super simple, but worth the tip.
The controller do the following:
- <?
- AppController { ControladorController class extends AppController (
- function myFunction () (
- ===- # Instantiate the class and pass the path as a parameter
- Folder ( 'img/home/' ) ; $ Folder = new Folder ('img / home /');
- # ===- Join the find () method of Folder class and pass a regex to search the archives
- -> find ( ".* \. png" ) ; $ Files = $ folder -> find (".* \. Png");
- # ===- Seven variable to use the way you want in the view
- ( 'arquivos' , $arquivos ) ; $ This -> set ('files', $ file);
- )
- )
- ?>
As you can see, the method find () 'hold' Regular Expression as a parameter. By default, the parameter is'.*', ie any character in any quantity.
In my case, I used the following regex: '.* \. Png'. I mean that file names that start with anything and with any number of characters (.*) and ending with. Png (\. Png). Then he will marry this: asdfasdfsfsfa.png or so: teste123.png or so: _teste_.png or so: another teste.png. It will ignore any file that does not end with '. Png'.
If you use only $ folder-> find (), all files in the folder are listed.
This method returns an array with the found files.
I hope I have helped, just post anything.


