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.

General Questions /

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

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

Using opendir inside page controller


Reply


8 Posts   823 Views

Avatar
knsqnt

Community Member, 85 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
knsqnt

Community Member, 85 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 [url=http://www.php.net/manual/en/function.opendir.php]opendir[/url] 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
knsqnt

Community Member, 85 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
knsqnt

Community Member, 85 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