Solved

How to chdir() more than once

Posted on 2009-04-06
2
924 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

691 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