PHP Unconference Europe 2015

chdir

(PHP 4, PHP 5)

chdirMuda o diretório

Descrição

bool chdir ( string $directory )

Muda o diretório atual do PHP para diretorio.

Parâmetros

directory

The new current directory

Valor Retornado

Retorna TRUE em caso de sucesso ou FALSE em caso de falha.

Exemplos

Exemplo #1 chdir() example

<?php

// diretório atual
echo getcwd() . "\n";

chdir('public_html');

// diretório atual
echo getcwd() . "\n";

?>

O exemplo acima irá imprimir algo similar à:

/home/vincent
/home/vincent/public_html

Notas

Nota: Quando o safe-mode está ativo, o PHP verifica se o diretório no qual o script está em operação tem o mesmo UID (proprietário) do script que está sendo executado.

Veja Também

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