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.
getcwd
(PHP 4, PHP 5)
getcwd — Ermittelt das aktuelle Arbeitsverzeichnis
Beschreibung
string getcwd
( void
)
Gibt das aktuelle Arbeitsverzeichnis zurück.
Rückgabewerte
Gibt bei Erfolg das aktuelle Arbeitsverzeichnis zurück, im Fehlerfall FALSE.
Auf einigen Unix-Varianten gibt getcwd() FALSE zurück,
wenn eines der darüber liegenden Verzeichnisse keine Lese- oder Suchrechte
haben, auch wenn das aktuelle Verzeichnis diese hat. Lesen Sie
chmod() für weitergehende Informationen über Modi und
Zugriffsrechte.
Beispiele
Beispiel #1 getcwd() Beispiel
<?php
// aktuelles Verzeichnis
echo getcwd() . "\n";
chdir('cvs');
// aktuelles Verzeichnis
echo getcwd() . "\n";
?>
Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
/home/didou /home/didou/cvs
getcwd
troy dot cregger at gmail dot com
26-Jan-2007 09:10
26-Jan-2007 09:10
mark dot phpnetspam at mhudson dot net
03-Nov-2006 10:42
03-Nov-2006 10:42
This function is often used in conjuction with basename(), i.e.
http://www.php.net/manual/en/function.basename.php
hodgman at ali dot com dot au
07-Sep-2006 04:58
07-Sep-2006 04:58
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.
emailfire at gmail dot com
08-Dec-2005 03:57
08-Dec-2005 03:57
To get the username of the account:
<?php
$dir = getcwd();
$part = explode('/', $dir);
$username = $part[1];
?>
If current directory is '/home/mike/public_html/' it would return mike.
memandeemail at gmail dot com
07-Dec-2005 12:48
07-Dec-2005 12:48
Some server's has security options to block the getcwd()
Alternate option:
str_replace($_SERVER['SCRIPT_NAME'],'', $_SERVER['SCRIPT_FILENAME']);
marcus at synchromedia dot co dot uk
04-May-2005 05:41
04-May-2005 05:41
"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.
vermicin at antispam dot gmail dot com
27-Jan-2005 12:48
27-Jan-2005 12:48
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.
manux at manux dot org
22-Jun-2004 06:44
22-Jun-2004 06:44
watch out:
working directory, and thus:
getcwd ()
is "/" while being into a register'ed shutdown function!!!
dave at corecomm dot us
10-Dec-2003 07:14
10-Dec-2003 07:14
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.
fvu at wanadoo dot nl
24-Nov-2003 02:50
24-Nov-2003 02:50
Make sure to lowercase the result before comparing Windows paths, because this function returns ambiguous results on Windows. From within Apache, the returned path is lowercase, while from the command line interface (CLI) the returned path uses the 'real' Windows pathname. For example, running the 'print getcwd();' command from 'C:\Program Files' returns either
c:\program files (Apache)
C:\Program Files (CLI)
When the directory is specified using chdir(), getcwd() uses the exact chdir argument. For example:
<?php chdir('C:\\PrOgRaM fIlEs'); print getcwd(); ?>
outputs:
C:\PrOgRaM fIlEs (Apache & CLI)
The following code can be used to return a unambiguous lowercased cwd when running on Windows:
<?php $sCwd = (substr(PHP_OS, 0, 3) == 'WIN') ? strtolower(getcwd()) : getcwd(); ?>
raja at rajashahed dot com
06-Nov-2003 07:21
06-Nov-2003 07:21
This is current working directory. X example, your document root is c:\Inetpub\www\htdocs. When You need to know what is your doc_root; /* Like ask yourself your name ;)*/
$_cur_dir = getcwd();
echo "My doc_root is $_cur_dir ";
// it prints out : My doc_root is c:\Inetpub\www\htdocs
/* Usually you need it after using chdir() to know what is
running in current directory */
Regards
Raja