ashez2ashes Posted March 29, 2012 Share Posted March 29, 2012 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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Directory Contents</title> </head> <body> <?php //Script 11.5 - list_dir.php date_default_timezone_set('America/New_York'); $search_dir = '../.'; $contents = scandir($search_dir); print '<h2>Directories</h2> <ul>'; //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 /> <h2>Files</h2> <table cellpadding="2" cellspacing="2" align="left"> <tr> <td>Name</td> <td>Size</td> <td>Last Modified</td> </tr>'; //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>$item</td> <td>$fs bytes</td> <td>$lm</td> </tr>\n"; } } print '</table>'; ?> </body> </html> Link to comment Share on other sites More sharing options...
ashez2ashes Posted March 29, 2012 Author Share Posted March 29, 2012 After looking through the PHP manuel, am I correct in that scandir doesn't scan the files in subdirectories? And to do what I want to do I'd have to make an array for each chapter folder or make a new function? Link to comment Share on other sites More sharing options...
Larry Posted March 30, 2012 Share Posted March 30, 2012 Yes, scandir() does not automatically go through the subdirectories. You'd need to use is_dir() to confirm an element is a directory and then apply scandir() to it, too. Link to comment Share on other sites More sharing options...
Recommended Posts