php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #14839 getcwd() and `pwd`, PHP behaviour nor clearly documented
Submitted: 2002-01-04 00:16 UTC Modified: 2002-06-10 19:04 UTC
From: php at chaska dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0.5 OS: Linux, FreeBSD, Solaris
Private report: No CVE-ID: None
 [2002-01-04 00:16 UTC] php at chaska dot com
This problem was previously reported by someone against PHP 4.0.6 in Bug ID # 14214.  Latest comment on the that reports says they are going to close it for lack of information.

This bug has existed in every version of PHP I've used since 1999.  It fails on Linux, FreeBSD and Solaris.

Both mistaken report the directory which contains the script being run, NOT the current working directory.

This fails when run from a directory other than where the script is saved:

#! /usr/local/bin/php -q
<?php
system("pwd");
echo `pwd`;
echo getcwd();
echo "\n";
?>

Under any POSIX-compliant OS, or just about any version of Unix, calling the getcwd(3) library routine should get the correct result.  It appears the PHP interpreter startup is changing directories without saving this value first, since even calling the OS gives incorrect values.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-04 00:22 UTC] php at chaska dot com
Oh, I suppose you want my configure line, even though it really doesn't make a difference.  Here it is:

CONFIGURE_COMMAND = './configure' '--with-gd=/usr/local' '--with-mysql' '--with-openssl' '--with-apxs=/usr/local/sbin/apxs' '--prefix=/usr/local'
 [2002-01-04 00:47 UTC] imajes@php.net
Ok, some comments, if i may.

Firstly, I have verified this behaviour, however (and I could be wrong), this is not unexpected.

The script executes relavtive to the directory it exists 
within, not the directory you are currently in. 

I can't particularly explain why, but this kind of makes sense. If you had a script in some unsafe directory, allowing the moving and deleting of files, and you could run it in a secured directory, in which the php process had sufficient access, and affect the files there, would that not present potential security risks?

Anyhow, i could be wrong, so don't take what i said as corret.
 [2002-01-04 05:01 UTC] edink@php.net
There is nothing wrong with getcwd(). The problem
is that php changes current working directory to 
the script's.

This makes sense if you think of CGI scripts,
but makes no sense if you write command line programs.

That's way a new command line switch (-C) was introduced
in PHP 4.1.0 which prevents PHP from chdir'ing into
script's directory.

So changing the first line of your script to

#!/usr/local/bin/php -qC

(and upgrading to 4.1) should fix the problem.

 [2002-01-04 05:02 UTC] edink@php.net
Closing the report.
 [2002-01-04 11:59 UTC] php at chaska dot com
"edink@php.net" makes it all clear.  I searched all the release documentation (for all the releases since getcwd() was introduced in 1999) prior to writing this bug for any remarks about this kind of behavior, but couldn't find anything.

The default behavior for Unix and DOS programs is to run in the CWD, so programmers tend to expect that of other programs, such as PHP.  As "imajes@php.net" and "edink@php.net" pointed out, this may not be the most desirable behavior for a Web script.  Since it is contrary to what most programs do, I'd like to request that the documentation of the startup behavior of PHP be sure to note this behavior, and that the -C option be documented a bit more boldly -- at least listed in the release notes.

Thanks.
 [2002-01-04 13:10 UTC] mfischer@php.net
Valid points .. I reopen this als doc thing.
 [2002-06-10 19:04 UTC] mfischer@php.net
This bug has been fixed in CVS. You can grab a snapshot of the
CVS version at http://snaps.php.net/. In case this was a documentation 
problem, the fix will show up soon at http://www.php.net/manual/.
In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites.
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 16 15:01:28 2024 UTC