您的位置:首页 > 其它

【ERROR_3】关于路径:绝对+相对路径混写的情况

2010-12-28 17:26 330 查看
近日在书中看到如下路径的写法,不太理解:

$document_root=$_SERVER['DOCUMENT_ROOT'];

$f=fopen("$document_root/../orders/orders.txt","rb");

今日弄清楚了,这是php绝对路径与相对路径混写的一种方式,它表示 $document_root上一级目录下的orders/orders.txt

例如:若$document_root的值为:D:/usr/apache/htdocs ,则:$document_root/../orders/orders.txt 表示的路径是:

D:/usr/apache/orders/orders.txt

测试代码:(注:D:/usr/apache/orders/orders.txt 为存在文件)

<?php
error_reporting(E_ALL);
$document_root=$_SERVER['DOCUMENT_ROOT'];
echo $document_root."<br>";  // 输出$document_root的值
$f=fopen("$document_root/../orders/orders.txt","rb");
if(!$f){
echo "error! 打开失败<br>";
exit;
}else{
echo "success!打开成功<br>";
fclose($f);
}

?>


输出结果:

D:/usr/apache/htdocs
success!打开成功

说明: ../ 这种写法不安全,建议少采用!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: