Solved

How to chdir() more than once

Posted on 2009-04-06
2
902 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
ID: 24081014
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
ID: 24081034
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

813 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