chdir

(PHP 3, PHP 4, PHP 5)

chdir -- 改变目录

说明

bool chdir ( string directory )

将 PHP 的当前目录改为 directory

参数

directory

新的当前目录

返回值

如果成功则返回 TRUE,失败则返回 FALSE

范例

例子 1. chdir() 例子

<?php

// current directory
echo getcwd() . "\n";

chdir('public_html');

// current directory
echo getcwd() . "\n";

?>

上例的输出类似于:

/home/vincent
/home/vincent/public_html

注释

注: 安全模式被激活时,PHP 将检查被操作的目录是否和正在执行的脚本有相同的 UID(所有者)。

参见

getcwd()


add a note add a note User Contributed Notes
jeprubio _At_ gmail dot com
27-May-2005 03: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)