chdir

(PHP 4, PHP 5, PHP 7)

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
8
nesk at xakep dot ru
4 years ago
When working with FFI under a PHP ZTS environment, there is no standard way to change the directory with libraries (dll/so/dylib/etc), so to get around this problem, you should use something like this polyfill:

<?php

$directory
= 'path/to/libraries';

switch (\
PHP_OS_FAMILY) {
    case
'Windows':
        \
FFI::cdef('extern unsigned char SetDllDirectoryA(const char* lpPathName);', 'kernel32.dll')
            ->
SetDllDirectoryA($directory)
        ;
        break;

    case
'Linux':
    case
'BSD':
        \
FFI::cdef('int setenv(const char *name, const char *value, int overwrite);')
            ->
setenv('LD_LIBRARY_PATH', $directory, 1)
        ;
        break;

    case
'Darwin':
        \
FFI::cdef('int setenv(const char *name, const char *value, int overwrite);')
            ->
setenv('DYLD_LIBRARY_PATH', $directory, 1)
        ;
        break;
}

?>
up
-13
php dot duke at qik dot nl
15 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"
up
-28
herwin at snt dot utwente dot nl
18 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)
To Top