Jump to:

23488 Posts in 18005 Topics by 2863 members

General Questions

SilverStripe Forums » General Questions » Using opendir inside page controller

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

Page: 1
Go to End
Author Topic: 743 Views
  • theGermaniac
    Avatar
    Community Member
    82 Posts

    Using opendir inside page controller Link to this post

    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

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: Using opendir inside page controller Link to this post

    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

  • theGermaniac
    Avatar
    Community Member
    82 Posts

    Re: Using opendir inside page controller Link to this post

    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?

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: Using opendir inside page controller Link to this post

    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...

  • theGermaniac
    Avatar
    Community Member
    82 Posts

    Re: Using opendir inside page controller Link to this post

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

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: Using opendir inside page controller Link to this post

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

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

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

    Cheers

  • theGermaniac
    Avatar
    Community Member
    82 Posts

    Re: Using opendir inside page controller Link to this post

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

  • Invader_Zim
    Avatar
    Community Member
    141 Posts

    Re: Using opendir inside page controller Link to this post

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

    Cheers

    743 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.