|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-10-02 19:00 UTC] ant at specialops dot ath dot cx
Description:
------------
In 5.3.0 one of my scripts stopped working due to the behaviour documented at the bottom of the register_shutdown_function page.
The actual problem is that if I change the directory in the shutdown function, that change persists across requests and can end up in completely unrelated scripts' shutdown function calls.
Reproduce code:
---------------
<?php
echo "Script A\n";
function test() {
echo getcwd() . "\n";
if ( file_exists('file.inc') ) {
include 'file.inc';
}
}
register_shutdown_function('test');
?>
<?php
echo "Script B\n";
function change() {
chdir('/tmp/');
file_put_contents('file.inc', '<?php echo "something nasty\n"; ?>');
}
register_shutdown_function('change');
?>
Expected result:
----------------
Script A
/var/www/localhost/htdocs/
Script B
Script A
/var/www/localhost/htdocs/
Actual result:
--------------
Script A
/var/www/localhost/htdocs/
Script B
Script A
/tmp
something nasty
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 07:00:01 2025 UTC |
We could store the cwd before trying to execute the shutdown handlers, and then restore it to that dir after the handlers are executed. However I'm not sure whether we should do this on per SAPI level or per function implementation level, below patch patches it per function level. Could you try apply this and see if it solves the problem? Index: basic_functions.c =================================================================== --- basic_functions.c (revision 289184) +++ basic_functions.c (working copy) @@ -5026,10 +5026,17 @@ void php_call_shutdown_functions(TSRMLS_D) /* {{{ */ { if (BG(user_shutdown_function_names)) { + char *cwd = getcwd(NULL, 0); + zend_try { zend_hash_apply(BG(user_shutdown_function_names), (apply_func_t) user_shutdown_function_call TSRMLS_CC); } zend_end_try(); + + if (cwd) { + chdir(cwd); + } + php_free_shutdown_functions(TSRMLS_C); } }