Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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?
Reply With Quote
  #2 (permalink)  
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/
Reply With Quote
  #3 (permalink)  
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/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:20 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.