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>';
}
chdir
(PHP 4, PHP 5)
chdir — ディレクトリを変更する
説明
bool chdir
( string $directory
)
PHP のカレントディレクトリを directory に変更します。
パラメータ
- directory
-
新しいカレントディレクトリ
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
例
例1 chdir() の例
<?php
// カレントディレクトリ
echo getcwd() . "\n";
chdir('public_html');
// カレントディレクトリ
echo getcwd() . "\n";
?>
上の例の出力は、たとえば 以下のようになります。
/home/vincent /home/vincent/public_html
注意
注意: セーフモード が有効の場合、PHP は、 操作を行うディレクトリが、実行するスクリプトと同じ UID (所有者)を有しているか どうかを確認します。
chdir
andy dot clark at dial dot pipex dot com
21-Aug-2006 05:10
21-Aug-2006 05:10
herwin at snt dot utwente dot nl
17-Aug-2006 08:58
17-Aug-2006 08:58
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)
jeprubio _At_ gmail dot com
27-May-2005 09:25
27-May-2005 09:25
If you run this script when $sym_dir is a symbolic link to a directory:
echo getcwd()."\n";
chdir ($sym_dir);
chdir ('../');
echo getcwd()."\n";
It returns for example:
/dades/loc/real/mapes/navteq/anloc
/mapes/anloc
It will not return to the previous directory, it returns to the parent of the real directory where it links the symbolic link.
It's not necessary a bug but I think it's important to have present this.
It could be solved saving the current directory and then returning to it. For example:
$cwd = getcwd();
echo getcwd()."\n";
chdir ($sym_dir);
chdir ($cwd);
echo getcwd()."\n";
It returns:
/dades/loc/real/mapes/navteq/anloc
/dades/loc/real/mapes/navteq/anloc
Have fun :)
-------
Josep Rubio (Anloc S.L. www.anloc.net)
