單詞功能
單詞用法
char *getcwd(char *buf, int n);
程式例子
#include <stdio.h>
#include <dir.h>
int main(void)
{
char buffer[MAXPATH];
getcwd(buffer, MAXPATH);
printf("The current directory is: %s\n", buffer);
return 0;
}
PHP getcwd函式
(PHP4,PHP5)
getcwd—取得當前工作目錄
說明
stringgetcwd(void)
取得當前工作目錄。
返回值
成功則返回當前工作目錄,失敗返回FALSE。
在某些Unix的變種下,如果任何父目錄沒有設定可讀或搜尋模式,即使當前目錄設定了,getcwd()還是會返回FALSE。有關模式與許可權的更多信息見chmod()。
範例
Example#1getcwd()例子
<?php
//currentdirectory
echogetcwd()."";
chdir('cvs');
//currentdirectory
echogetcwd()."";
?>
以上例程的輸出類似於:
/home/didou
/home/didou/cvs
參見
chdir()-改變目錄
chmod()-改變檔案模式
PHPgetcwdnote#1
Asyoucouldreadin
theCLISAPIdoes-contrarytootherSAPIs-NOTautomaticallychangethecurrentworkingdirectorytotheonethestartedscriptresidesin.
Averysimpleworkaroundtoregainthebehaviouryou'reusedtofromyour"ordinary"webpagescriptingistoincludesomethinglikethatatthebeginningofyourscript:
<?php
chdir(dirname(__FILE__));
?>
Butbecausethisisaboutreadingor"finding"pathes,youmightappreciateitifIsharesomemoresophisticatedtricksIfrequentlyuseinCLIscripts...
<?php
//Note:allpathesstoredinsubsequentVariablesendupwithaDIRECTORY_SEPARATOR
//howtostoretheworkingdirectory"fromwhere"thescriptwascalled:
$initial_cwd=preg_replace('~(w)$~','$1'.DIRECTORY_SEPARATOR,realpath(getcwd()));
//howtoswitchsymlink-freetothefolderthecurrentfileresidesin:
chdir(dirname(realpath(__FILE__)));
//howtostoretheformerfolderinavariable:
$my_folder=dirname(realpath(__FILE__)).DIRECTORY_SEPARATOR;
//howtogetapathonefolderupif$my_folderendswithclassor/class/:
$my_parent_folder=preg_replace('~[/\]class[/\]$~',DIRECTORY_SEPARATOR,$my_folder);
//howtogetapathonefolderupinanycase:
$my_parent_folder=preg_replace('~[/\][^/\]*[/\]$~',DIRECTORY_SEPARATOR,$my_folder);
//howtomakeanarrayofOS-style-pathesfromanarrayofunix-style-pathes
//(handyifyouuseconfig-filesorso):
foreach($unix_style_pathesas$unix_style_path)
$os_independent_path[]=str_replace('/',DIRECTORY_SEPARATOR,$unix_style_path);
?>