PHP Doku:: Wechseln des Verzeichnisses - function.chdir.html

Verlauf / Chronik / History: (7) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDateisystemrelevante ErweiterungenVerzeichnis-FunktionenVerzeichnis-Funktionenchdir

Ein Service von Reinhard Neidl - Webprogrammierung.

Verzeichnis-Funktionen

<<Verzeichnis-Funktionen

chroot>>

chdir

(PHP 4, PHP 5)

chdirWechseln des Verzeichnisses

Beschreibung

bool chdir ( string $directory )

Wechselt das aktuelle Verzeichnis von PHP zu directory.

Parameter-Liste

directory

Das neue aktuelle Verzeichnis

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Beispiele

Beispiel #1 chdir() Beispiel

<?php

// aktuelles Verzeichnis
echo getcwd() . "\n";

chdir('public_html');

// aktuelles Verzeichnis
echo getcwd() . "\n";

?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

/home/vincent
/home/vincent/public_html

Anmerkungen

Hinweis: Wenn Safe Mode aktiviert ist überprüft PHP, ob das Arbeitsverzeichnis, die gleiche UID (Eigentümer) hat wie das Skript selbst.

Siehe auch

  • getcwd() - Ermittelt das aktuelle Arbeitsverzeichnis


4 BenutzerBeiträge:
- Beiträge aktualisieren...
php dot duke at qik dot nl
31.01.2009 23:16
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"
andy dot clark at dial dot pipex dot com
21.08.2006 17:10
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>';
}
herwin at snt dot utwente dot nl
17.08.2006 20: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.05.2005 9: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)



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)