Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 16th, 2003, 07:21 AM
ade ade is offline
Registered User
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Beginning php 4 chapter 10 problems

i keep coming across numerous errors when using the scripts on a windows server, most of them work but a few dont. I've checked the errata there's nothing there.

chapter 10's nav_dir.php

its meant to give a list of folders and files in them but i get

Traversing docs.....

Warning: opendir(docs) [function.opendir]: failed to open dir: Invalid argument in C:\Program Files\Apache Group\Apache2\htdocs\test\nav_dir.php on line 7
can't open docs


<?php
//nav_dir.php
    $default_dir = "docs";
    function traverse_dir($dir) {
    echo "Traversing $dir..... <br>";
    chdir($dir);
    if(!($dp = opendir($dir))) die ("can't open $dir.");

    while($file = readdir($dp)) {
    if(is_dir($file)) {
    if($file != '.' && $file != '..') {
    echo "$file<br>";
    traverse_dir("$dir/$file");
    chdir($dir);
    }
    }
    else echo "$file<br>";
    }
    closedir($dp);
    }
    traverse_dir($default_dir);
 ?>

thats the code

i've notice that i get an extra error if i use / in the $default_dir = "/docs";

this must be something to do with windows platform filing system.
i've tried turning magic quotes off in the php.ini and this didnt help

any suggestions?
 
Old June 19th, 2003, 02:33 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, I wouldn't expect this to work on ANY machine because of these two adjacent lines:

chdir($dir);
if(!($dp = opendir($dir))) die ("can't open $dir.");

opendir() looks for $dir to be a subdir of the current working directory... but you change the working diretory with chdir() on the previous line. That means if $dir is "docs", that function will only work if you have a directory named docs/docs beneath the current working directory, and even then it will traverse the nested docs, not the outer one.


Take care,

Nik
http://www.bigaction.org/
 
Old June 19th, 2003, 02:43 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's a slightly modified version of your original script that shows how to properly deal with recursive calls, chdir, and opendir:

Code:
<?php 
$default_dir = "docs"; 

function traverse_dir($dir, $indent = 0)
{
    $ind_str = str_repeat(" &nbsp; &nbsp;", $indent);

    chdir($dir);
    if(!($dp = opendir('.'))) die ("{$ind_str}can't open $dir.\n");

    while($file = readdir($dp))
    {
        if(is_dir($file))
        {
            if($file != '.' && $file != '..')
            {
                echo "{$ind_str}<b>$file</b><br>\n";
                traverse_dir("$file", $indent + 1);
            }
        }
        else echo "{$ind_str}$file<br>\n";
    }
    closedir($dp);
    chdir('..');
}

traverse_dir($default_dir); 
?>
Take care,

Nik
http://www.bigaction.org/




Similar Threads
Thread Thread Starter Forum Replies Last Post
chapter 10 Emailing with php - firstmail.php vijdev BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 July 19th, 2007 01:12 AM
Beginning Visual C# Exercises - Chapter 10 seblake C# 1 July 29th, 2004 05:10 PM
Beginning php 4 chapter 10 problems dakey Beginning PHP 3 February 13th, 2004 06:21 PM
Chapter 10 - Beginning VB6 database programming darryck VB Databases Basics 3 October 13th, 2003 03:40 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.