headkit
headkit

Reputation: 3327

get a list of all files inside a directory outside the system folder

i am new to yii and i need to get a list of files inside a directory outside of the system folder. i tried it with

        $fileListOfDirectory = array();
        $pathTofileListDirectory = Yii::app()->request->baseUrl.'/path/to/files';
        foreach( new DirectoryIterator($pathTofileListDirectory) as $file) {
            if( $file->isFile() === TRUE && $file->getBasename() !== '.DS_Store' && substr(strrchr($file->getPathname(), '.'), 1) == 'txt') {
                array_push($fileListOfDirectory, $file->getBasename());
            }
        }

but I allways get the error

DirectoryIterator::__construct(/myYiiInstallation/system/path/to/files) [directoryiterator.--construct]: failed to open dir: No such file or directory

from the line

foreach( new DirectoryIterator($pathTofileListDirectory) as $file) {

So is there another way to get the list of files or what should I do? thnx!

Upvotes: 0

Views: 3329

Answers (2)

Baba
Baba

Reputation: 95131

You can use this ....

$fileListOfDirectory = array ();
$pathTofileListDirectory = Yii::app()->request->baseUrl.'/path/to/files' ;

if(!is_dir($pathTofileListDirectory ))
{
    die(" Invalid Directory");
}

if(!is_readable($pathTofileListDirectory ))
{
    die("You don't have permission to read Directory");
}

foreach ( new DirectoryIterator ( $pathTofileListDirectory ) as $file ) {
    if ($file->isFile () === TRUE && $file->getBasename () !== '.DS_Store') {

        if ($file->getExtension () == "txt") {
            array_push ( $fileListOfDirectory, $file->getBasename () );
        }
    }
}

Upvotes: 1

schmunk
schmunk

Reputation: 4708

I think you should try http://www.yiiframework.com/doc/api/1.1/CApplication#basePath-detail instead of using the baseUrl in your file system.

Upvotes: 2

Related Questions