Solved

How to chdir() more than once

Posted on 2009-04-06
2
886 Views
Last Modified: 2012-05-06
I have a file.php that before I include it, use chdir() to resource from a subfolder. However file.php calls chdir() as well, for its includes and it throws:

Warning: chdir() [function.chdir]: No error (errno 0) in C:\wamp\www\a\test.php on line 4
Warning: include(test2.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\a\test.php on line 5
Warning: include() [function.include]: Failed opening 'test2.php' for inclusion (include_path='.;C:\php5\pear') in C:\wamp\www\rob\a\test.php on line 5

Below is the three files i'm using to test this:
------------------ C:\wamp\www\index.php -------------------------

<?php
 

echo 'Changing dir to a...<br />';

chdir('a/');

include('test.php');
 

?>
 

------------------ C:\wamp\www\a\test.php -------------------------

I'm in folder 'a'<br />

<?php 

echo 'changing folder to b...';

chdir('b/');

include ('test2.php');

 ?>
 
 

------------------ C:\wamp\www\b\test2.php -------------------------

i'm in folder b

Open in new window

0
Comment
Question by:level9wizard
2 Comments
 
LVL 17

Accepted Solution

by:
nplib earned 500 total points
Comment Utility
it's because you have to orient our self to the new directory

so the first chdir()

puts you from

EXAMPLE:
/public_html/www

to
/public_html/www/a

which is now your working directory

so chir('b/'); being called after words is trying to change to

/public_html/www/a/b
which probably doesn't exist.

you need to  change it to

chdir('../b/');

which will then change it to
/public_html/www/b



0
 
LVL 10

Expert Comment

by:Phatzer
Comment Utility
I'm not sure include will take in to account the chdir before it because I've never needed to do it that way. Also, what is the reason you are doing it that way instead of:

include('a/test.php');

or

include ('b/test2.php');

?
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now