SitePoint Sponsor

User Tag List

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

    how to create a list of files in a folder

    I have searched using Google and searched on this site. It may be that I just don't have the right terms for my search, but...

    I am trying to build a site where various people can login to get stored pdf documents. One or two people will be uploading the pdf and others will routinely login to get/read the files.

    I know how to use php to get the authorization level and direct the administrators to an upload page, and have standard users directed to a different page.

    Is there a way to get php to build a list of files in a specific folder on the website and display this list?
    Each day is a learning experience.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    There sure is, take a look at the DirectoryIterator

    There are examples in the comments on that page.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,367
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    Another way is to use glob: PHP: glob - Manual

  4. #4
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I now have something to play with and learn how to use.

    I had not thought of searching on iterate and would never have thought of glob.

    I am sure I will be back with questions.
    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)
    Well, I had the script working, and failed to realize it was locked on only three files. I created an upload form and uploaded a couple of pdf files that do not show up in the list.
    PHP Code:
    <?php
    foreach (new DirectoryIterator('../documents') as $fileInfo) {
        if(
    $fileInfo->isDot()) continue;
        echo 
    '<a href="/documents/' $fileInfo->getFilename() .  ' ">' $fileInfo->getFilename() .  '</a><br> ';
    }
    ?>
    This worked once, and I do not remember changing this, but is there an error here?
    Each day is a learning experience.

  6. #6
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CSU-Bill View Post
    Well, I had the script working, and failed to realize it was locked on only three files. I created an upload form and uploaded a couple of pdf files that do not show up in the list.
    PHP Code:
    <?php
    foreach (new DirectoryIterator('../documents') as $fileInfo) {
        if(
    $fileInfo->isDot()) continue;
        echo 
    '<a href="/documents/' $fileInfo->getFilename() .  ' ">' $fileInfo->getFilename() .  '</a><br> ';
    }
    ?>
    This worked once, and I do not remember changing this, but is there an error here?
    i think this is the problem..
    PHP Code:
    foreach (new DirectoryIterator('../documents') as $fileInfo


    try to instantiate the class to a variable first before placing it in a loop..
    PHP Code:
    $di = new DirectoryIterator('../documents');
    foreach (
    $di as $fileInfo
    and also i think that new DirectoryIterator('../documents') will not return an array..
    maybe you need to execute a method first?

    PHP Code:
    $di = new DirectoryIterator('../documents');
    foreach (
    $di->getSomething() as $fileInfo

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is embarrassing.

    I had somehow created two document folders in different locations. I have been uploading to the wrong folder, so the problem is my upload form pointing to the wrong directory.

    Guess I should be more awake when I am writing code.
    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
  •