CakePHP - Search / list files in a folder


1 Estrelas2 Estrelas3 Estrelas4 Estrelas5 Estrelas (9 votes, average: 4.56 out of 5)
Loading ... Loading ...

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:

  1. <?
  2. AppController { ControladorController class extends AppController (
  3. function myFunction () (
  4. ===- # Instantiate the class and pass the path as a parameter
  5. Folder ( 'img/home/' ) ; $ Folder = new Folder ('img / home /');
  6. # ===- Join the find () method of Folder class and pass a regex to search the archives
  7. -> find ( ".* \. png" ) ; $ Files = $ folder -> find (".* \. Png");
  8. # ===- Seven variable to use the way you want in the view
  9. ( 'arquivos' , $arquivos ) ; $ This -> set ('files', $ file);
  10. )
  11. )
  12. ?>

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.

CakePHP - How to use the table name with nonstandard


1 Estrelas2 Estrelas3 Estrelas4 Estrelas5 Estrelas (5 votes, average: 5.00 out of 5)
Loading ... Loading ...

Well, everybody knows that to create a controller in CakePHP you must create the table in the database named in the plural. This is very easy, but, since I came across a driver that is called 'default' ... so the Cake hopes that the name is 'options' right? Wrong! The 'dictionary' of Ceke Cake is English then that would put the table name as 'option'.

Another situation that can happen is you get a site / system to reprogram with Ceke Cakemas can not lose the data from the database ... Many times the bank ADDO is already structured and non-standard names that the Cake wait.

For this, there is a very simple solution. We will use the $ useTable attribute and put the table name. See example below:

  1. <?
  2. AppModel { class Option extends AppModel (
  3. = "Opcao" ; var $ name = "Option";
  4. = "opcoes" ; var $ useTable = "options";
  5. )
  6. ?>

There you go, making this the problem I had was resolved. If the table's primary key is different than 'id', use the attribute $ primaryKey, putting the field name.

I hope I helped, hugs!

Get Adobe Flash player Plugin by wpburn.com wordpress themes