List and display files in a folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kkhan5000
    New Member
    • Jul 2008
    • 8

    List and display files in a folder

    Hello,

    I am new to PHP and have put together the following code just by copying other examples from the web. Now I am stuck and would like to sort and display files alphabetically, and also filter what file type to display or not display. Thank you for your help.

    -kkhan5000
    [code=php]
    <?php
    /*
    ABOUT:
    This snippet will list all the files in the directory of your
    choice, truncate the file extension, capitalize the first letter and
    put it all in a drop down menu. The script will not list subdirectories so
    all you see are the files in your directory.

    USAGE:
    Change the $dirpath variable the directory you want to list.


    */

    //Looks into the directory and returns the files, no subdirectories

    //The path to the style directory

    $dirpath = ".";
    $dh = opendir($dirpat h);
    while (false !== ($file = readdir($dh))) {

    //Don't list subdirectories
    if (!is_dir("$dirp ath/$file"))
    {

    //Truncate the file extension and capitalize the first letter
    echo "<a href='$dirpath/$file'>". htmlspecialchar s(ucfirst(preg_ replace('/\..*$/', '', $file))). "<br />";"</a>";

    }
    }
    closedir($dh);
    ?>[/code]
    Last edited by pbmods; Jul 4 '08, 01:57 AM. Reason: Added code tags.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    To sort them alphabetically, you'd have to create an array that holds the file name and location. Then sort() the array.

    Comment

    • kkhan5000
      New Member
      • Jul 2008
      • 8

      #3
      Thank you markusn00b, I will start learning about arrays.
      -kkhan5000

      Comment

      • kkhan5000
        New Member
        • Jul 2008
        • 8

        #4
        Help!

        I am not sure if I can figure this out on my own.

        kkhan5000

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Read about arrays.

          Comment

          • kkhan5000
            New Member
            • Jul 2008
            • 8

            #6
            Thank you for the help with arrays. It is working now as far as the sorting is concerned, but it is now showing the sub directories. How do I filter out folders and file types. Here is what I have now:

            <?php
            $dirpath = ".";
            $dh = opendir($dirpat h);
            while (false !== ($file = readdir($dh))) {
            $fileList[] = trim($file);

            //Don't list subdirectories
            if (!is_dir("$dirp ath/$file")){
            sort ($fileList); // sort the file list in the array
            }
            }
            reset ($fileList); // go back to the top of the array
            while (list ($key, $file) = each ($fileList))
            {
            //Truncate the file extension and capitalize the first letter
            echo "<a href='$dirpath/$file'>". htmlspecialchar s(ucfirst(preg_ replace('/\..*$/', '', $file))). "<br />";"</a>";


            }
            closedir($dh);
            ?>

            Comment

            • dlite922
              Recognized Expert Top Contributor
              • Dec 2007
              • 1586

              #7
              Originally posted by kkhan5000
              Code:
              <?php
              ...
              //Don't list subdirectories
              if (!is_dir("$dirpath/$file")){
               	sort ($fileList); // sort the file list in the array
              }
              ...
              I'm no expert, but maybe where your code says "Don't list subdirectories" has something to do with it.

              Check out this code:



              If you have any questions implementing that, let us know!


              -Dan

              Comment

              • kkhan5000
                New Member
                • Jul 2008
                • 8

                #8
                Thank you Dan,

                I fixed the subdirectory problem by re-organizing the code. Now the only last thing left is to display only a certain type of files instead of everything the folder.

                Thank you,

                -kk

                Comment

                • Markus
                  Recognized Expert Expert
                  • Jun 2007
                  • 6092

                  #9
                  Originally posted by kkhan5000
                  Thank you Dan,

                  I fixed the subdirectory problem by re-organizing the code. Now the only last thing left is to display only a certain type of files instead of everything the folder.

                  Thank you,

                  -kk
                  You could get the file extension and then check to see whether it's in an array of allowed extensions.

                  Comment

                  • kkhan5000
                    New Member
                    • Jul 2008
                    • 8

                    #10
                    Thank you markusn00b ,

                    I think I got it to work somewhat by using "!is_file". Well, I have something now what I needed and I can use. However, during the process of learning php, my requirements have considerabley changed. I am not a coder, period, and would like to pay if needed to get the following done.

                    I would like to use all lower case file names, which are hyphenated or underscored like: "nice_day.p hp or nice-day.htm". And, I would like to display them as "Nice Day" in a web page or navigation bar. The goal is to drop an php/html document in a folder which dynamically shows up as a link in a navigation area of the web page.

                    Any advice would be greatly appreciated.

                    -kk

                    Comment

                    Working...