Programming Basics
  Home arrow Programming Basics arrow Reading Directorys with PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PROGRAMMING BASICS

Reading Directorys with PHP
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-11-01

    Table of Contents:
  • Reading Directorys with PHP
  • The final code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Reading Directorys with PHP


    (Page 1 of 2 )

    A quick tutorial on how to read in the contents of a directory and display them.

    By : Sliver

    In this tutorial I'm going to show you how to create a directory reading system. A directory reading sytem will display all the contents of the specified directory automatically w/o you having to change your index file.

    You can use this for things like an mp3 list so that if you frequently change the contents of your mp3 directory you dont have to change your index page all the time.

    Lets look at the first bit of code:

    <TABLE ALIGN=CENTER BGCOLOR=#ffffff CELLPADDING=4
    CELLSPACING=0 
    BORDER=2>
    <tr><TH>File</TH><TH>Size</TH></TR>
    <?php
      $dir="./";
      if (is_dir($dir)) {

    First we define our table and headings. Then we start the script. Next we specify the directory to read(./=current directory). Last we check if the directory is a valid directory.

        $fd = @opendir($dir);
        if($fd) {
          while (($part = @readdir($fd)) == true) {
            if ($part != "." && $part != "..") {
              $file_array[]=$part;
            }
          }
        }

    Now we set a variable value to open the dir. After that we loop through the dir and name $part to the file name. Next we set an arrays value to $part(wich is the files name). Finally we close the if and the loop.

        sort($file_array);
        reset($file_array);

    This sorts the array alphabetically.

        for($i=0;$i<count($file_array);$i++) {
          $npart=$file_array[$i];
          if (!strstr($npart,".inc.php") && !strstr($npart,"index.php")) {
            $fsize=filesize($npart)/1000;

    This starts a loop makes a new variable called $npart and sets it to the file_arrays value. Next we make sure its not an include file and it is not the index file if its not we get the file size and put it into kilobytes.

          if (!strstr($npart,".inc.php") && !strstr($npart,"index.php")) {
            $fsize=filesize($npart)/1000;
            if (is_dir($npart)) {
              print("<tr><TD><a href=\"$npart\">$npart</a></TD><TD>Directory</TD></TR>\n");
            } else {
              print("<tr><TD><a href=\"$npart\">$npart</a></TD><TD>$fsize KB</TD></TR>\n");
            }
          }
        }
      }
    ?>

    Now we check if it is a directory. If it is it prints a link to it and in the size column it prints Directory. If it is not a directory we print a link to the file and print the size of the file in KB(or kilobytes).

    More Programming Basics Articles
    More By Codewalkers


       · just a couple of things that i did to get this excellent script to work for me.i...
       · I tried this code but I'm getting a parse error in the line...
       · I tried this code but I'm getting a parse error in the line...
       · what can i do to remove the file size
       · Instead display the dir result on table how can display the listing in a menu...
     

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek