php点点通
,欢迎大家一起探讨php教程!
Loading
您所在的位置:首页 > 转载文章 > 正文

PHP chdir(),chdir函数 改变目录

作者: 来源: 日期:2016-10-21 15:30:44 人气:56 加入收藏 评论:0 标签:

PHP chdir 改变目录

chdir

(PHP 4, PHP 5)

chdir — 改变目录

说明

bool chdir ( string $directory )

将 PHP 的当前目录改为 directory

参数


  • directory

  • 新的当前目录


返回值

成功时返回 TRUE, 或者在失败时返回 FALSE.

范例

Example #1 chdir() 例子

<?php

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

chdir('public_html');

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

?>

以上例程的输出类似于:

/home/vincent
/home/vincent/public_html


注释

Note当启用 安全模式时, PHP 会在执行脚本时检查被脚本操作的目录是否与被执行的脚本有相同的 UID(所有者)。

参见


  • getcwd() - 取得当前工作目录



« ceilcheckdate »

PHP chdir note #1

When changing dir's under windows environments: 

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

$path="c: emp"'

chdir($path); 
/* getcwd() gives you back "c: emp" */ 
?> 

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

PHP chdir note #2

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>';
}

PHP chdir note #3

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)

PHP chdir note #4

If you run this script when $sym_dir is a symbolic link to a directory:
  echo getcwd()." ";
  chdir ($sym_dir);
  chdir ('');
  echo getcwd()." ";

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()." ";
  chdir ($sym_dir);
  chdir ($cwd);
  echo getcwd()." ";

It returns:
  /dades/loc/real/mapes/navteq/anloc
  /dades/loc/real/mapes/navteq/anloc

Have fun :)
-------
   Josep Rubio (Anloc S.L. www.anloc.net)


    本文网址:http://www.phpddt.com/2016category/reprint/56.html
    读完这篇文章后,您心情如何?
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    更多>>网友评论
    发表评论