Reputation: 3327
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
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
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