SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    trying to use DirectoryIterator to link to filies

    I have recently learned about DirectoryIntegrator (thanks to this forum) and have read the PHP manual. The code I found as a starter works to list the files.
    PHP Code:
    <?php
    foreach (new DirectoryIterator('../myfolder') as $fileInfo) {
        if(
    $fileInfo->isDot()) continue;
        echo 
    $fileInfo->getFilename() . "<br>\n";
    }
    ?>
    I then tried to get the list of files and provide a link to the files. That is where my lack of knowledge showed up and I have a RuntimeException with my code.

    PHP Code:
    <?php
    foreach (new DirectoryIterator('../myfolder') as $fileInfo) {
        if(
    $fileInfo->isDot()) continue;
        echo 
    '<a href="/myfolder/' $fileInfo->getFilename() .  ' ">' $fileInfo->getFilename() .  '</a><br> ';
    }
    ?>
    What am I doing wrong with the echo statement?
    Each day is a learning experience.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I can see no errors in that echo statement. What exactly does the error say?

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    The exception will have a message and a stack trace associated with it, with those we can help further.

    P.S. If you're using PHP 5.3 (you should be) then the FilesystemIterator should be used, which would negate the need for an isDot() check.
    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Salathe,

    I will have to ask my host to upgrade me to PHP 5.3. I currently have PHP 5.2.17.

    The error is:
    Fatal error: Uncaught exception 'RuntimeException' with message 'DirectoryIterator::__construct(../diocese) [<a href='directoryiterator.--construct'>directoryiterator.--construct</a>]: failed to open dir: No such file or directory' in /diocese/clergy/directory_list.html:15 Stack trace: #0 /diocese/clergy/directory_list.html(15): DirectoryIterator->__construct('../diocese') #1 {main} thrown in /diocese/clergy/directory_list.html on line 15
    If I change the echo statement to the un-modified echo, it will produce a list of files.
    Each day is a learning experience.

  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found my problem, and it was a simple spelling error on the folder holding the files.
    Each day is a learning experience.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •