Help With Cfilehelper

Hi folks.

I have a jQuery slider that is fed by HTML imgs, which is iterates. I am trying to create a script in a controller that will grab the images in a certain directory and return an array which I will then decorate with the HTML and send to the view to be ECHOed via iterating the array.

I am trying to use CFileHelper and I have the following code…




$the_path = Yii::app()->request->baseUrl."/images/";

CFileHelper::findFiles($the_path);



However, this is bringing back an error. $the_path brings back a valid path of "public_root/images", but I get the following error…

opendir(/public_root/images/,/public_root/images/): The system cannot find the path specified. (code: 3)

Can anyone help me out with this? It would be most appreciated as there doesn’t appear to be many examples of CFileHelper in action out there!

The method you’re calling requires a file system path, not a URL. Try this instead:




$path = Yii::getPathOfAlias('webroot.images');

$files = CFileHelper::findFiles($path);



Thank you, Sir.

I am currently developing this on local host, and the URL for the file being brought back is c:\xampp\htdocs… etc. Will this be any issue at all when moved to a public server? I would imagine not?

No, it should work the same on the live server.

Thank you.

Any idea on how I go about passing that array into the layout I have created for that page? I can’t seem to do it.

I have figured it all out except for one thing - the array of files it is bring back I am echoing as src inside img tags. However, it is bringing back back an array with a path separator of "\" instead of "/". Is there any way to sort this at the route? Or do I just need to substitute in string as required when I iterating it in the view?

BTW I assume the reason for the issue is that either "/" or "\" is accepted by PHP where as HTML rendering will only accept "/"?

Does that even work? The files should be uploading to a protected location. You’d need to copy them to a web accessible location to be able to access them by URL. Having moved them to a web accessible location, you’d use baseUrl or an equivalent to product a URL with the correct path separator.

Thanks mate, I’ll give that a shot.

BTW is there a specific folder inside of website>protected that should be used? I mean in terms of best practice.

For storing images? PHP will initially save it to a temporary folder on the file system. If you want it to be accessible, you should save it to a web accessible location. If you want it to be protected, you should save it to somewhere like protected/images.