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
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: