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);


		if(isset($_POST['Blog']))

		{

			$model->attributes=$_POST['Blog'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


		$this->render('create',array(

			'model'=>$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

Hi,

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';

:(

Use


dirname(Yii::app()->request->scriptFile).'/images'

See

http://www.yiiframework.com/forum/index.php/topic/4955-basepath/

http://www.yiiframework.com/forum/index.php?/topic/536-basepath-url/page__hl__scriptFile%20__fromsearch__1

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