getcwd

(PHP 4, PHP 5, PHP 7, PHP 8)

getcwd β€” ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ Ρ‚Π΅ΠΊΡƒΡ‰ΡƒΡŽ Ρ€Π°Π±ΠΎΡ‡ΡƒΡŽ Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΎΡ€ΠΈΡŽ

ОписаниС

function getcwd(): string|false

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌΡƒ Ρ€Π°Π±ΠΎΡ‡Π΅ΠΌΡƒ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Ρƒ.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

Π‘ΠΈΠ³Π½Π°Ρ‚ΡƒΡ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π½Π΅ содСрТит ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ².

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ ΠΏΡƒΡ‚ΡŒ ΠΊ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌΡƒ Ρ€Π°Π±ΠΎΡ‡Π΅ΠΌΡƒ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Ρƒ, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

На ΠΎΡ‚Π΄Π΅Π»ΡŒΠ½Ρ‹Ρ… Unix-ΠΏΠ»Π°Ρ‚Ρ„ΠΎΡ€ΠΌΠ°Ρ… функция getcwd() Π²Π΅Ρ€Π½Ρ‘Ρ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ false, Ссли Π½Π° ΠΊΠ°ΠΊΠΎΠΌ-Π»ΠΈΠ±ΠΎ ΠΈΠ· Ρ€ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΡΠΊΠΈΡ… ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³ΠΎΠ² Π½Π΅ установили Ρ€Π΅ΠΆΠΈΠΌ чтСния ΠΈΠ»ΠΈ поиска, Π΄Π°ΠΆΠ΅ Ссли Ρ€Π΅ΠΆΠΈΠΌ установили Π½Π° Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π΅. ΠŸΠΎΠ΄Ρ€ΠΎΠ±Π½Π΅Π΅ ΠΎ Ρ€Π΅ΠΆΠΈΠΌΠ°Ρ… доступа рассказываСт описаниС Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ chmod().

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ получСния ΠΏΡƒΡ‚ΠΈ ΠΊ Ρ‚Π΅ΠΊΡƒΡ‰Π΅ΠΌΡƒ Ρ€Π°Π±ΠΎΡ‡Π΅ΠΌΡƒ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Ρƒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ getcwd()

<?php

// Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³
echo getcwd() . "\n";

chdir('cvs');

// Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³
echo getcwd() . "\n";

Π’Ρ‹Π²ΠΎΠ΄ ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π° Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΡ…ΠΎΠΆ Π½Π°:

/home/didou
/home/didou/cvs

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

ΠŸΡ€Π΅Π΄ΠΎΡΡ‚Π΅Ρ€Π΅ΠΆΠ΅Π½ΠΈΠ΅

Π’Π΅ΠΊΡƒΡ‰ΠΈΠΉ Ρ€Π°Π±ΠΎΡ‡ΠΈΠΉ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ функция getcwd(), ΠΈΠ½ΠΎΠ³Π΄Π° отличаСтся ΠΎΡ‚ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π°, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°ΡŽΡ‚ интСрфСйсы ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΎΠ½Π½ΠΎΠΉ систСмы, Ссли PHP-ΠΈΠ½Ρ‚Π΅Ρ€ΠΏΡ€Π΅Ρ‚Π°Ρ‚ΠΎΡ€ собрали с ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠΎΠΉ потокобСзопасного Ρ€Π΅ΠΆΠΈΠΌΠ° ZTS (Π°Π½Π³Π». Zend Thread Safety). Π­Ρ‚ΠΎ Π·Π°Ρ‚Ρ€ΠΎΠ½Π΅Ρ‚ внСшниС Π±ΠΈΠ±Π»ΠΈΠΎΡ‚Π΅ΠΊΠΈ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π²Ρ‹Π·Ρ‹Π²Π°ΡŽΡ‚ΡΡ Ρ‡Π΅Ρ€Π΅Π· ΠΌΠΎΠ΄ΡƒΠ»ΡŒ FFI ΠΈ ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ зависят ΠΎΡ‚ Ρ‚Π΅ΠΊΡƒΡ‰Π΅Π³ΠΎ Ρ€Π°Π±ΠΎΡ‡Π΅Π³ΠΎ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³Π°.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • chdir() - Π˜Π·ΠΌΠ΅Π½ΡΠ΅Ρ‚ Ρ€Π°Π±ΠΎΡ‡ΠΈΠΉ ΠΊΠ°Ρ‚Π°Π»ΠΎΠ³
  • chmod() - Π˜Π·ΠΌΠ΅Π½ΡΠ΅Ρ‚ Ρ€Π΅ΠΆΠΈΠΌ доступа ΠΊ Ρ„Π°ΠΉΠ»Ρƒ
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 18 notes

up
44
dave at corecomm dot us ΒΆ
22 years ago
getcwd() returns the path of the "main" script referenced in the URL.

dirname(__FILE__) will return the path of the script currently executing.

I had written a script that required several class definition scripts from the same directory. It retrieved them based on filename matches and used getcwd to figure out where they were.

Didn't work so well when I needed to call that first script from a new file in a different directory.
up
6
marcus at synchromedia dot co dot uk ΒΆ
21 years ago
"On some Unix variants, getcwd() will return FALSE if any one of the parent directories does not have the readable or search mode set, even if the current directory does."

Just so you know, MacOS X is one of these variants (at least 10.4 is for me). You can make it work by applying 'chmod a+rx' to all folders from your site folder upwards.
up
10
hodgman at ali dot com dot au ΒΆ
19 years ago
I use this code to replicate the pushd and popd DOS commands in PHP:

<?php
$g_DirStack = array();
function pushd( $dir )
{
    global $g_DirStack;
    array_push( $g_DirStack, getcwd() );
    chdir( $dir );
}
function popd( )
{
    global $g_DirStack;
    $dir = array_pop( $g_DirStack );
    assert( $dir !== null );
    chdir( $dir );
}
?>

This allows you to change the current directory with pushd, then use popd to "undo" the directory change when you're done.
up
2
CXJ ΒΆ
9 years ago
getcwd() appears to call the equivalent of PHP's realpath() on the path.  It never returns symlinks, but always the actual directory names in the path to the current working directory.
up
3
Anonymous ΒΆ
4 years ago
given a link
/some/link->/some/location/path

with linux bash,
if  within the linked drawer  /some/link
cd ..              goes upper link                   /some/ 
cd -P ..         goes upper destination       /some/location/

with php
fopen ("../file")  goes upper destination        /some/location/file

some others commented about ways obtaining the path below.

I found some luck with using    $_SERVER['DOCUMENT_ROOT'] instead
to recraft an absolute path.
up
10
ash at ashmckenzie dot org ΒΆ
17 years ago
It appears there is a change in functionality in PHP5 from PHP4 when using the CLI tool.  Here is the example: -

cd /tmp

cat > foo.php << END
<?php
    print getcwd() . "\n";
?>
END

cd /

php -q /tmp/foo.php

PHP4 returns /tmp
PHP5 returns /

Something to be aware of.
up
10
znupi69NOSPAMHERE at gmail dot com ΒΆ
18 years ago
When running PHP on the command line, if you want to include another file which is in the same directory as the main script, doing just
<?php
include './otherfile.php';
?>
might not work, if you run your script like this:
/$ /path/to/script.php
because the current working dir will be set to '/', and the file '/otherfile.php' does not exist, because it is in '/path/to/otherfile.php'.
So, to get the directory in which the script resides, you can use this function:
<?php
function get_file_dir() {
    global $argv;
    $dir = dirname(getcwd() . '/' . $argv[0]);
    $curDir = getcwd();
    chdir($dir);
    $dir = getcwd();
    chdir($curDir);
    return $dir;
}
?>
So you can use it like this:
<?php
include get_file_dir() . '/otherfile.php';
// or even..
chdir(get_file_dir());
include './otherfile.php';
?>
Spent some time thinking this one out, maybe it helps someone :)
up
6
mark dot phpnetspam at mhudson dot net ΒΆ
19 years ago
This function is often used in conjuction with basename(), i.e.
http://www.php.net/manual/en/function.basename.php
up
5
Craig ΒΆ
10 years ago
Be aware when calling getcwd() in directories consisting of symlinks.

getcwd()  is the equivalent of shell command "pwd -P" which resolves symlinks.

The shell command "pwd" is the equivalent of "pwd -L" which uses PWD from the environment without resolving symlinks. This is also the equivalent of calling getenv('PWD').
up
7
leonbrussels at gmail dot com ΒΆ
18 years ago
This is a function to convert a path which looks something like this:

/home/www/somefolder/../someotherfolder/../

To a proper directory path:

<?php

function simplify_path($path) {

//saves our current working directory to a variable
$oldcwd = getcwd();
//changes the directory to the one to convert
//$path is the directory to convert (clean up), handed over to the //function as a string

chdir($path);
return gstr_replace('\\', '/', getcwd());

//change the cwd back to the old value to not interfere with the script
chdir($oldcwd);

}

This function is really useful if you want to compare two filepaths which are not necesarily in a "cleaned up" state. It works in *NIX and WINDOWS alike

?>
up
6
troy dot cregger at gmail dot com ΒΆ
19 years ago
Take care if you use getcwd() in file that you'll need to include (using include, require, or *_once) in a script located outside of the same directory tree. 

example: 
<?php
//in /var/www/main_document_root/include/MySQL.inc.php
if (strpos(getcwd(),'main_')>0) {
  //code to set up main DB connection
}
?>

<?php
//in home/cron_user/maintenance_scripts/some_maintenance_script.php
require_once ('/var/www/main_document_root/include/MySQL.inc.php');
?>

In the above example, the database connection will not be made because the call to getcwd() returns the path relative to the calling script ( /home/cron_user/maintenance_scripts ) NOT relative to the file where the getcwd() function is called.
up
6
memandeemail at gmail dot com ΒΆ
20 years ago
Some server's has security options to block the getcwd()

Alternate option:

str_replace($_SERVER['SCRIPT_NAME'],'', $_SERVER['SCRIPT_FILENAME']);
up
7
bvidinli at gmail dot com ΒΆ
17 years ago
if you link your php to /bin/linkedphp  and your php is at for ex /home/actual.php

when you run linkedphp in somewhere in your filesystem,
getcwd returns /bin instead of working dir,

solution: use dirname(__FILENAME__) instead
up
4
znupi69NOSPAMHERE at gmail dot com ΒΆ
18 years ago
In response to myself: that function will not work for cases like:
/usr/bin$: /home/johndoe/Work/script.php
So here's a better and simpler way (I think this one works for all cases)
<?php
function get_file_dir() {
    global $argv;
    return realpath($argv[0]);
}
?>
Knock yourself out :)
up
6
Anonymous ΒΆ
15 years ago
As you could read in
http://www.php.net/manual/en/features.commandline.differences.php
the CLI SAPI does - contrary to other SAPIs - NOT automatically change the current working directory to the one the started script resides in.

A very simple workaround to regain the behaviour you're used to from your "ordinary" webpage scripting is to include something like that at the beginning of your script:

<?php
  chdir( dirname ( __FILE__ ) );
?>

But because this is about reading or "finding" pathes, you might appreciate it if I share some more sophisticated tricks I frequently use in CLI scripts ...

<?php
// Note: all pathes stored in subsequent Variables end up with a DIRECTORY_SEPARATOR

// how to store the working directory "from where" the script was called:
$initial_cwd = preg_replace( '~(\w)$~' , '$1' . DIRECTORY_SEPARATOR , realpath( getcwd() ) );

// how to switch symlink-free to the folder the current file resides in:
chdir( dirname ( realpath ( __FILE__ ) ) );

// how to store the former folder in a variable:
$my_folder = dirname( realpath( __FILE__ ) ) . DIRECTORY_SEPARATOR;

// how to get a path one folder up if $my_folder ends with \class\ or /class/ :
$my_parent_folder = preg_replace( '~[/\\\\]class[/\\\\]$~' , DIRECTORY_SEPARATOR , $my_folder );

// how to get a path one folder up in any case :
$my_parent_folder = preg_replace( '~[/\\\\][^/\\\\]*[/\\\\]$~' , DIRECTORY_SEPARATOR , $my_folder );

// how to make an array of OS-style-pathes from an array of unix-style-pathes
// (handy if you use config-files or so):
foreach( $unix_style_pathes as $unix_style_path )
    $os_independent_path[] = str_replace( '/' , DIRECTORY_SEPARATOR , $unix_style_path );

?>
up
3
manux at manux dot org ΒΆ
21 years ago
watch out:
working directory, and thus:
getcwd () 
is "/" while being into a register'ed shutdown function!!!
up
1
ab5602 at wayne dot edu ΒΆ
18 years ago
If getcwd() returns nothing for you under Solaris with an NFS mounted subdirectory, you are running into an OS bug that is supposedly fixed in recent versions of Solaris 10.  This same OS bug effects the include() and require() functions as well.
up
1
vermicin at antispam dot gmail dot com ΒΆ
21 years ago
If your PHP cli binary is built as a cgi binary (check with php_sapi_name), the cwd functions differently than you might expect. 

say you have a script /usr/local/bin/purge
you are in /home/username 

php CLI: getcwd() gives you /home/username
php CGI: getcwd() gives you /usr/local/bin

This can trip you up if you're writing command line scripts with php. You can override the CGI behavior by adding -C to the php call: 

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

and then getcwd() behaves as it does in the CLI-compiled version.