Home arrow PHP arrow Page 4 - Uploading Files and Navigating Directories in PHP

Navigating Directories - PHP

In this article we are going to look at how to upload files and also how to navigate through directories. It is the second part of a tutorial that began last week with "Reading, Writing, and Creating Files in PHP."

TABLE OF CONTENTS:
  1. Uploading Files and Navigating Directories in PHP
  2. Uploading files
  3. Multiple file uploads
  4. Navigating Directories
By: Jacques Noah
Rating: starstarstarstarstar / 8
August 30, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before we start with the next section, please make sure that you are familiar with file permissions. Pretty much everything in the File Permissions section applies to the directories. To navigate through a directory, you begin by opening the directory with opdir() function:

$dir = opendir('directoryname');

As with reading and writing files, you create a pointer that can be referenced by the opened directory. To read the list of files within a directory you use the readdir() function, in a while loop:

while($thedir=readdir($dir)){
//write the file details here
}

and then you finally close the open directory with the closedir() function:

closedir($dir);

Other functions that we are going to use include:

 filesize() function - gives you the size of  the file in bytes.

filetime() - retrieves the modification time of a file.

Both of these functions are going to help us to create a directory listing.

Let's build a script that will show us the insides of a directory listing:

Script: browsedir.php

This script will list file names, file size and modification dates:

<html>
<head>
  <title>Browse Directories</title>
</head>
<body>
<p>Browsing</p>
<?php
  $current_dir = '.';
  $dir = opendir($current_dir);
echo "Current directory is <b>$current_dir</b><br />";
echo' <table width="100%" border="0" cellspacing="1">
  <tr>
    <td width="34%"><b>File Name</b> </td>
    <td width="26%"> <b>File Size </b></td>
    <td width="40%"><b>Last Modified</b> </td>
  </tr>';
  while ($file = readdir($dir))
  {
  $fs=filesize($file);
  $moddate=date('F j, Y',filemtime($file));
echo ' <tr>
    <td>'.$file.'</td>
    <td>'.$fs. ' bytes</td>
    <td>'.$moddate.'</td>';              

  }
   closedir($dir);
?>
  </tr>
  </table>
</body>
</html>

Fig5. Shows a list of all the files in the current directory, represented here by a dot(.) 

Conclusion

This concludes our file and directory handling exploration. 



 
 
>>> More PHP Articles          >>> More By Jacques Noah
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: