©
本文档使用 PHP中文网手册 发布
(PHP 4, PHP 5, PHP 7)
chdir — 改变目录
$directory
)
将 PHP 的当前目录改为
directory
。
directory
新的当前目录
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Throws an error of level E_WARNING
on failure.
Example #1 chdir() 例子
<?php
// current directory
echo getcwd () . "\n" ;
chdir ( 'public_html' );
// current directory
echo getcwd () . "\n" ;
?>
以上例程的输出类似于:
/home/vincent /home/vincent/public_html
Note: 当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。
[#1] php dot duke at qik dot nl [2009-01-31 14:16:06]
When changing dir's under windows environments:
<?php
$path="c:\temp"';
chdir($path);
$path="c:\temp\"';
chdir($path);
?>
to work around this inconsistency
doing a chdir('.') after the chdir always gives back "c:\temp"
[#2] andy dot clark at dial dot pipex dot com [2006-08-21 08:10:14]
This only changes the directory for PHP, the output directory stays the same. If you are trying to access images from a relative path and you use the following then it will fail to render the image:
chdir ('images');
if (file_exists('php.gif'))
{
echo '<html>';
echo '<body>';
echo '<img src="php.gif">';
echo '</body></html>';
}
//However, it is possible to use the <base> tag in the header to change the directory for the resulting HTML, as you can see however this requires you to put the full path in place.
chdir ('images');
if (file_exists('php.gif'))
{
echo '<html>';
echo '<head><base href = "http://uk.php.net/images/"></head>';
echo '<body>';
echo '<img src="php.gif">';
echo '</body></html>';
}
[#3] herwin at snt dot utwente dot nl [2006-08-17 11:58:12]
When using PHP safe mode and trying to change to a dir that is not accessible due to the safe mode restrictions, the function simply fails without generating any kind of error message.
(Tested in PHP 4.3.10-16, Debian Sarge default)