chdir

(PHP 4, PHP 5)

chdir디렉토리 변경

설명

bool chdir ( string $directory )

PHP의 현재 디렉토리를 directory로 변경합니다.

인수

directory

새 현재 디렉토리

반환값

성공할 경우 TRUE를, 실패할 경우 FALSE를 반환합니다.

예제

Example #1 chdir() 예제

<?php

// 현재 디렉토리
echo getcwd() . "\n";

chdir('public_html');

// 현재 디렉토리
echo getcwd() . "\n";

?>

위 예제의 출력 예시:

/home/vincent
/home/vincent/public_html

주의

Note: 안전 모드를 활성화했을 경우, PHP는 작업하려는 디렉토리가 실행중인 스크립트와 같은 UID(owner)를 가지고 있는지 확인합니다.

참고

  • getcwd() - 현재 작업 디렉토리를 얻음

add a note add a note

User Contributed Notes 3 notes

up
1
andy dot clark at dial dot pipex dot com
8 years ago
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>';
}
up
1
herwin at snt dot utwente dot nl
8 years ago
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)
up
0
php dot duke at qik dot nl
5 years ago
When changing dir's under windows environments:

<?php
$path
="c:\temp"';
chdir($path);
/* getcwd() gives you back "c:\temp" */

$path="c:\temp\"'
;
chdir($path);
/* getcwd() gives you back "c:\temp\" */
?>

to work around this inconsistency
doing a chdir('.') after the chdir always gives back "c:\temp"
To Top