PHP
  Home arrow PHP arrow Page 2 - Stream Me Up, Scotty (part 2)
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
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? 
PHP

Stream Me Up, Scotty (part 2)
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2000-11-29

    Table of Contents:
  • Stream Me Up, Scotty (part 2)
  • Looking Around
  • To Create And Destroy
  • Upsa-daisy!
  • Remote Control
  • The Application

  • 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


    Stream Me Up, Scotty (part 2) - Looking Around


    (Page 2 of 6 )

    Let's start at the top. Before you can get around to copying and moving files, you first need to be able to see them. And that's where PHP's directory commands come in - they provide you with an easy way to view and manipulate the contents of a specific directory on the system.

    In order to access the contents of a directory, you first need to open a "handle" to the directory with the opendir() function; you can then read and manipulate the contents with readdir(). Once you're done, it's good programming practice to close the handle you've just opened with closedir().

    The following example demonstrates these three steps:

    <? // get directory handle $hook = opendir("/tmp"); // read directory and echo list while (readdir($hook)) { $file = readdir($hook); echo "$file<br>"; } // close directory closedir($hook); ?>
    And you should see something like this:

    .. index.html .XF86Setup686 something.doc file.jpg
    A more professional approach is to write it the way the manual recommends.

    <? // get directory handle $hook = opendir("/tmp"); // read directory and echo list while (($file = readdir($hook))!==false) { echo "$file<br>"; } // close directory closedir($hook); ?>


    This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=The Object-Oriented Approach} Another approach to obtain a directory listing is to use the PHP "dir" class, which creates a directory object with its own properties and functions. Take a look:

    <? // get directory handle $hook = dir("/tmp"); // display location echo "<b>Current path is $hook->path</b><br>"; // read directory and echo list while ($file=$hook->read()) { echo "$file<br>"; } // close directory $hook->close(); ?>
    In this case, three functions are available - read(), close() and rewind() - together with two properties - $path, which records the current location in the filesystem, and $handle, which stores the resource ID for the directory handle.

    And if you take a close look at the output from the two examples above, you'll see that PHP typically also displays the parent and current directories in the listing (look for the . and .. symbols.) As you'll see when I get to the composite example, it's much neater to filter these two items out of the directory listing.

    <? // get directory handle $hook = dir("/tmp"); // display location echo "<b>Current path is $hook->path</b><br>"; // read directory and echo list while ($file=$hook->read()) { if ($file != "." && $file != "..") { echo "$file<br>"; } } // close directory $hook->close(); ?>
    And the output is:

    index.html .XF86Setup686 something.doc file.jpg
    Much neater!

    Finally, there's the chdir() function, which simply changes to a specific directory. You can combine this with what you've just seen to create a very simple file browser - take a look:

    <html> <head> <basefont face="Arial"> </head> <body> <? // if form has not been submitted, display form if (!$submit) { ?> <center> <form action=<? echo $PHP_SELF; ?> method=post> Enter location: <input type=text name=location size=15> <input type=submit name=submit value="Go"> </form> </center> <? } else { // change to directory $res = chdir($location); // if chdir() successful if ($res) { // get directory handle $hook = dir($location); // display location echo "<b>Current path is $hook->path</b><br>"; // read directory and echo list while ($file=$hook->read()) { if ($file != "." && $file != "..") { echo "$file<br>"; } } // close directory $hook->close(); } else { // display error echo "<b>Unable to locate directory $location</b><br>"; } } ?> </body> </html>


    This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=The Truth About Files And Directories} Now, let's move to files. As you've seen, the functions above simply display a combined listing of files and sub-directories in the selected directory. How about separating the two?

    Well, PHP comes with a bunch of functions which let you distinguish between files and directories, and even between different types of files. The following snippet uses the is_dir() function to display directories in a different colour:

    ... <? // get directory handle $hook = opendir($location); // read directory and echo list while (($file = readdir($hook)) !== false) { if ($file != "." && $file != "..") { // set up full path $path = $location . "/" . $file; // check for directory if (is_dir($path)) { echo "<font color=Red>$file</font><br>"; } else { echo "<font color=Green>$file</font><br>"; } } } // close directory closedir($hook); ?> ...
    In this case, the is_dir() function is used to identify whether or not a directory is being listed. PHP also offers the is_file() and is_link() function to identify files and links respectively.

    You can also obtain information on the listed files - size, permissions, ownership and the like. For example, the following code snippet shows you how to use the is_readable(), is_writeable() and is_executable() functions to display file properties, and the filesize() function to obtain the size of the file in bytes.

    ... <? // get directory handle $hook = opendir($location); // read directory and echo list while (($file = readdir($hook)) !== false) { if ($file != "." && $file != "..") { // set up full path $path = $location . "/" . $file; echo "<table border=1 cellspacing=2>"; // set up attribute list $att = ""; if (is_readable($path)) { $att .= "r"; } if (is_writeable($path)) { $att .= "w"; } if (is_executable($path)) { $att .= "x"; } // check for directory if (is_dir($path)) { // print file information echo "<tr><td width=200><font color=red>$file</font></td><td width=100>0 bytes</td><td width=100>$att</td></tr>"; } else { // get file size $size = filesize($path); // print file information echo "<tr><td width=200>$file</td><td width=100>$size bytes</td><td width=100>$att</td></tr>"; } } } // close directory closedir($hook); echo "</table>"; ?> ...


    This article copyright Melonfire 2000. All rights reserved.

    More PHP Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway