13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using Directory Functions<br />

441<br />

If you are making directory browsing available via this mechanism, it is sensible to<br />

limit the directories that can be browsed so that a user cannot browse directory listings<br />

in areas not normally available to him.<br />

An associated <strong>and</strong> sometimes useful function is rewinddir($dir), which resets the<br />

reading of filenames to the beginning of the directory.<br />

As an alternative to these functions, you can use the dir class provided by <strong>PHP</strong>. It has<br />

the properties h<strong>and</strong>le <strong>and</strong> path, <strong>and</strong> the methods read(), close(), <strong>and</strong> rewind(),<br />

which perform identically to the nonclass alternatives.<br />

In Listing 19.4 we rewrite the above example using the dir class.<br />

Listing 19.4<br />

browsedir2.php—Using the dir Class to Display the Directory Listing<br />

<br />

<br />

Browse Directories<br />

<br />

<br />

Browsing<br />

<br />

<br />

<br />

The filenames in the above example aren’t sorted in any particular order, so if you<br />

require a sorted list, you should use a function called sc<strong>and</strong>ir() that was introduced in<br />

<strong>PHP</strong> 5.This function can be used to store the filenames in an array <strong>and</strong> sort them in<br />

alphabetical order, either ascending or descending, as in Listing 19.5.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!