php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #26073 ob_start callback function changes cwd
Submitted: 2003-11-01 21:29 UTC Modified: 2004-09-16 16:24 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: php at trancer dot nl Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.3.3 OS: Debian Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at trancer dot nl
New email:
PHP Version: OS:

 

 [2003-11-01 21:29 UTC] php at trancer dot nl
Description:
------------
A script that does file_get_contents or file in the callback function of ob_start wont work. The file_get_contents function cant open the file. 

<?php 
    // Ob callback 
    function ob($buffer) 
    { 
        return file_get_contents('open.txt'); 
    } 
    ob_start('ob'); 
?>

The behaviour is so because the callback sets its cwd to the root of the disk instead of the folder the script was called from. 

If I copy the file I want to open to my root it can open, otherwise it will throw me this error.

Warning: file_get_contents(open.txt): failed to open stream: No such file
or directory in /var/www/test.php on line 6 

Specifying the full path works. Could be a possible bug in the callback, otherwise a documentation problem because I was unable to find it anywhere in the docs.


Reproduce code:
---------------
<?php 
    // Ob callback 
    function ob($buffer) 
    { 
        return file_get_contents('open.txt'); 
    } 
    ob_start('ob'); 
?>

Expected result:
----------------
The content of open.txt

Actual result:
--------------
Warning: file_get_contents(open.txt): failed to open stream: No such file
or directory in /var/www/test.php on line 6 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-03 19:32 UTC] sniper@php.net
It's not PHP that changes the cwd but Apache.

 [2003-12-04 08:48 UTC] Leblanc at phpzipscript dot org
here is a workaround I use to avoid such problems (I also use it with CLI scripts to avoid path troubles when launching a script from a different directory).

At the beginning of the script add :

<?php
define("SCRIPTPATH", str_replace("\\", "/", realpath(dirname(__FILE__))));
?>


in your ob() callback you now need to do :

<?php 
    // Ob callback 
    function ob($buffer) 
    { 
        return file_get_contents(SCRIPTPATH.'/open.txt'); 
    } 
    ob_start('ob'); 
?>

/Leblanc
 [2004-09-16 16:24 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"Some web servers (e.g. Apache) change the working directory of a script when calling the callback function. You can change it back by e.g. chdir(dirname($_SERVER['PHP_SELF'])) in the callback function."
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 14:01:33 2025 UTC