File Iterator

Hi guys,

I have an images folder outside protected folder and I want to create a dropdownlist listing the filenames from that folder to the view

Here’s what I did,

public function actionCreate()


		$model=new Blog;

		$files = new FilesystemIterator(Yii::app()->baseUrl . '/images/');

		$images = new RegexIterator($files, '/\.(?:jpg|png|gif)$/i');

		foreach($images as $image) {

			$filename = $image->getName();


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);









			'filename' => $filename,



However when I run it, It says No such file or directory. I’m not sure how to get the files in the images folder.

I believe its not a good practice to use those classes in yii (FilesystemIterator). Can you guys give me a better approach rather than this. I would really appreciate it. Thank you.

I haven’t use the iterators much, but I think you’ve got a problem here:

		foreach($images as $image) {

			$filename = $image->getName();


should probably be something like

                $filenames = array()

		foreach($images as $image) {

			$filenames[] = $image->getName();


You’ll need to change the render call accordingly


There’s actually an error

[font="Verdana"][size="4"]FilesystemItera__construct(/jam_blog/images/): failed to open dir: No such file or directory[/size][/font]

Im’ not sure if why i’ts not being recognized. The

Yii::app()->request->baseUrl . '/images';





If in doubt, run a print() on your result to check that the folder path is correct