Jump to content
Larry Ullman's Book Forums

I'M Stumped. Ch 11 - List_Dir.Php

Recommended Posts

I'm confused. I'm on ch11 script 11.5 list_dir.php. I've been structuring my files for the books exercises differently than what is the book default. So far I've been able to adjust the code, but I'm stumped this time. The file structure I'm using on my xampp server (with htdocs being the webroot) is: htdocs/phpbook/ch1/file.php or htdocs/phpbook/ch2/file.php etc. I want the script to display all of the exercies I've done so far in the book in all of the chapter folders.


The file list_dir.php is located at htdocs/phpbook/ch11/list_dir.php . I got it to show the chapter directories, but I can't get it to show the files within those chapter folders. I thought changing the directory in the second foreach loop to: $search . '/../' . $item would work but it does not. Any help you can give me would be really apprettiated.





<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...ransitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Directory Contents</title>




<?php //Script 11.5 - list_dir.php



$search_dir = '../.';

$contents = scandir($search_dir);


print '<h2>Directories</h2>



//For each loop for directories.

foreach($contents as $item) {

if ( (is_dir($search_dir . '/' . $item)) AND (substr($item, 0, 1) != '.') ) {

print "<li>$item</li>\n";





print '</ul>';


//Make a table for the files

print '<hr />


<table cellpadding="2" cellspacing="2" align="left">




<td>Last Modified</td>





//Foreach loop for files.

foreach($contents as $item) {

if( (is_file($search_dir . '/' . $item)) AND (substr($item, 0, 1) != '.')) {

$fs = filesize($search_dir . '/' . $item);

$lm = date('F j, Y', filemtime ($search_dir . '/' . $item));

print "<tr>


<td>$fs bytes</td>






print '</table>';






Link to comment
Share on other sites


  • Create New...