Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Using opendir inside page controller


Go to End


8 Posts   1608 Views

Avatar
MarioHofer

Community Member, 107 Posts

4 July 2011 at 10:58pm

Hi.

I have a project folder and inside this project folder I have the folder "screenshots". Via the backend, I can select the project folder. Now I want to create an array with the url all files inside the screenshots folder, but I can't open the directory :-(

Here is my code:

public function Screenshots() {
	$path = $this->BaseHref().$this->ProjectFolderPath().'screenshots/';
	$files = array();
	
	if($handle = opendir($path)) {
		while($file = readdir($handle)) {
			if ($file != "." && $file != "..") {
				array_push($files, $file);
			}
		}
		closedir($handle);
		return $files;
	} else {
		echo 'failed to open directory';
		return false;
	}
}

public function ProjectFolderPath() {
	$folder = DataObject::get_by_id('Folder', $this->ProjectFolderID);
	return $folder->getRelativePath();
}

Has anyone an idea of what the problem is...? Thanks in advance.

Cheers, Mario

Avatar
Invader_Zim

Community Member, 141 Posts

5 July 2011 at 12:39am

Hi Mario,

what happens when you place a Debug::show($path); after your
$path = $this->BaseHref().$this->ProjectFolderPath().'screenshots/';?

Or in other words... are you sure, $path is correct/leads to an actual path?

Cheers
Christian

Avatar
MarioHofer

Community Member, 107 Posts

5 July 2011 at 1:50am

Edited: 05/07/2011 1:50am

Yes, the path is correct.

When I enable debugging, I get this:

[Warning] opendir(http://new.knsqnt.com/assets/Portfolio/Brooke-Fraser/screenshots/) [function.opendir]: failed to open dir: not implemented

Can you figure out a bit better than me what it means?

Avatar
Invader_Zim

Community Member, 141 Posts

5 July 2011 at 2:40am

Uh, oh... this is the first time i see this error message (but a great chance to challenge my Google-Fu!)

I searched for the error a bit and if i understand opendir correctly, it
doesn't work with an Url, except if directory listing is enabled on the server.

So, maybe directory listing is not enabled on your server?

I keep investigating...

Avatar
MarioHofer

Community Member, 107 Posts

5 July 2011 at 2:42am

Ah, good hint! Let's see who's faster...

Avatar
Invader_Zim

Community Member, 141 Posts

5 July 2011 at 5:20am

Not quite sure on this, but what happens if you use:

$path = Director::baseFolder() . '/' . $this->ProjectFolderPath() . 'screenshots/';

instead of your $path = $this->BaseHref... ?

Cheers

Avatar
MarioHofer

Community Member, 107 Posts

5 July 2011 at 5:41am

Sweet Dude! You rock! Work perfectly! Great! Thanks! Thanks! Thanks!

Avatar
Invader_Zim

Community Member, 141 Posts

5 July 2011 at 5:44am

Wohooo great! And I also learned something new from this thread! *throws confetti*

Cheers