duanjing4623 2013-01-29 16:37
浏览 7
已采纳

php中的/../和../之间有区别吗?

I was curious if ../ and /../ is the same in PHP so I tried these:

require_once('/../frame/header.php');
require_once('../frame/header.php');

Both of them worked. Was I just lucky or they realy do the same exact thing? Why?

  • 写回答

2条回答 默认 最新

  • duanniying2342 2013-01-29 16:39
    关注

    In your context if they both worked, that implies that your scripts are at the root of the filesystem you have access to, or one level in. They are not the same however! /../ refers to the filesystem root (and .. one directory up, which just gets eaten and is still the root), while ../ refers to one directory higher than the current one. Any path beginning with / is an absolute path from the filesystem root.

    From anywhere other than the filesystem root, these would not function equivalently.

    Suppose your working directory is /var/www/scripts.

    require_once('../include.php');
    

    Will include a file at /var/www/include.php.

    But from that same location, if you did

    require_once('/../include.php');
    

    ...php will attempt to load the file /include.php at the filesystem root, and it probably won't exist.

    Now, a lot of web hosts will supply you with a filesystem whose root / is also the web server's document root, the web server document root is only one directory level in from the root like /www. In that case, /../ may work fine, but beware if you ever attempt to move it to another server with a different filesystem configuration.

    So if the script's working directory was /www, just by luck, these two would function the same way:

    require_once('../include.php');
    require_once('/../include.php');
    

    Both would include the file /include.php at the filesystem root.

    Note, this is not PHP-specific, but a property of filesystems which use the .. in general...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度