?
Solved

How to chdir() more than once

Posted on 2009-04-06
2
Medium Priority
?
973 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 …
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

807 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