LoginSignup
2
2

More than 5 years have passed since last update.

CakePHPで任意のディレクトリ下にあるファイル一覧を取得する

Posted at

簡単な使い方

HogesController.php
//フォルダーユーティリティーを読み込む
App::uses('Folder', 'Utility');

public function index() {
    //インスタンスを作成
    $dir = new Folder('path/dir');
    $files = $dir->read();
}

/* 

返却される配列の[0]番目にディレクトリが格納され、[1]番目にファイルリストが格納される。

$files = array(
    [0] => array(
        [0] => 'hogedir', 
        [1] => 'fugadir', 
    ),
    [1] => array(
        [0] => 'hogehoge.png', 
        [1] => 'fugafuga.png', 
        [2] => 'piyopiyo'.png
    ) 
);
*/

こんだけ。

補足情報

Folderインスタンスの生成

 $dir = new Folder(WWW_ROOT.'path/',true,0755);
  • インスタンス化する際に、ディレクトリのパスを指定する。(デフォルトはapp\tmp)
  • 第2引数にtrueを指定すると、ディレクトリが存在しない場合は新規作成する(デフォルトはfalse)。
  • 第3引数にはディレクトリのパーミッションを指定できる。(デフォルトは0755)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2