V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iscraft
V2EX  ›  PHP

php5.4+和 php7.0 的包含路径问题 实在找不到原因啊啊啊啊啊啊

  •  
  •   iscraft · 2016-12-29 13:12:24 +08:00 · 4043 次点击
    这是一个创建于 2647 天前的主题,其中的信息可能已经有所发展或是发生改变。
    网站根目录 index.php 中

    include "/test.php";
    在 5.4 以及 5.6 下正常执行
    在 7.0 下则 failed to open stream: No such file or directory

    include "test.php";
    在 3 个版本下都正常没问题

    从早上 google 到现在都没找到 php7 的哪部分新特性和遗弃功能里有讲这个的 求教!
    27 条回复    2016-12-29 17:51:09 +08:00
    batnss
        1
    batnss  
       2016-12-29 13:17:40 +08:00
    get_include_path() 看看
    iscraft
        2
    iscraft  
    OP
       2016-12-29 13:25:28 +08:00
    @batnss 5 和 7 下都是.;C:\php\pear
    ncwgf
        3
    ncwgf  
       2016-12-29 13:30:28 +08:00
    忘了哪時開始
    我都養成習慣寫
    `include implode(DIRECTORY_SEPARATOR, array(dirname(__FILE__), 'test.php'));`
    因為要假設控制不了環境
    短一點
    `include implode(DIRECTORY_SEPARATOR, [__DIR__, 'test.php']);`
    實在不知何時能找到 何時會找不到
    skydiver
        4
    skydiver  
       2016-12-29 13:38:55 +08:00
    不应该是 ./test.php 吗……
    harborM
        5
    harborM  
       2016-12-29 13:40:00 +08:00
    实在不行就用绝对路径呗
    Famio
        6
    Famio  
       2016-12-29 13:42:08 +08:00
    @skydiver 我也同你一样,第一反应是./xxx.php
    但是我没用过 7 ,没把握回答不敢乱写。。
    iscraft
        7
    iscraft  
    OP
       2016-12-29 13:46:02 +08:00
    @ncwgf 这也是一个不错的办法 预定义根目录路径常量也行 但就是以前写的一部分代码里面使用了 /这种 想找一条捷径都支持而不必再去修改代码
    @skydiver ./跟不加 /是一样的 都指向当前所在目录路径 我的疑问在 7 为什么不支持 /的根目录绝对路径用法
    msg7086
        8
    msg7086  
       2016-12-29 13:48:03 +08:00
    / 指向的是文件系统根目录吧?
    iscraft
        9
    iscraft  
    OP
       2016-12-29 13:54:34 +08:00
    @msg7086 但 5.4 和 5.6 都会在 index.php 所在的 E:\desktop\www 下找到 test.php 唯独 7 会报出错误
    Scholer
        10
    Scholer  
       2016-12-29 13:56:14 +08:00
    nginx 里 root 配的不一致吧
    iscraft
        11
    iscraft  
    OP
       2016-12-29 14:04:07 +08:00
    @Scholer 使用的是 windows 下的 apache FreeBSD 中 Caddy+php7 下加 /报错 不加正常执行 个人感觉跟 web server 关系不大 与 php 版本有关
    skydiver
        12
    skydiver  
       2016-12-29 14:06:16 +08:00
    @iscraft 这个写法本来就不对,就不应该这么写。之前能用也许是 bug ,新版修复了
    yaxin
        13
    yaxin  
       2016-12-29 14:14:23 +08:00
    把 php-fpm 开成一个,然后 strace -p fpm 子进程 id ,看一下到底加载的那个文件
    iscraft
        14
    iscraft  
    OP
       2016-12-29 14:17:00 +08:00
    @skydiver 后面会避免这种错误写法 只是想看看官方或者一些正式的说法 毕竟这情况至少在 5.2.10 就出现了
    wesley
        15
    wesley  
       2016-12-29 15:02:41 +08:00
    这也怪 php ?
    先搞清楚 URL 路径和文件系统路劲
    greatonce
        16
    greatonce  
       2016-12-29 15:14:59 +08:00
    ubuntu nginx php-7.0.13 下面没有问题
    iscraft
        17
    iscraft  
    OP
       2016-12-29 15:29:38 +08:00
    @wesley
    才疏学浅 不敢怪 一点小疑问 还请不吝赐教
    /是作为 url 路径表示网站根目录而不能作为文件系统路径表示 DocumentRoot 的
    那么 为什么 5+可以依据这个符号包含进来文件 而 7 不可以?
    求相关的官方或者非官方文档解释都可以 谢谢
    iscraft
        18
    iscraft  
    OP
       2016-12-29 15:32:32 +08:00
    @greatonce 谢谢 我也去 vmware 个试试
    Felldeadbird
        19
    Felldeadbird  
       2016-12-29 15:38:22 +08:00
    你想 /test.php 指向当前 运行目录的根目录,那么你就的让 apache 、 nginx 、 PHP 添加对应的配置信息。 具体配置你可以网上找一下。
    一般来说,这种写法有很大问题,因为太过依赖配置的信息,一旦换了一个人或者一个服务器新的配置,那么你的程序就黄了。
    greatonce
        20
    greatonce  
       2016-12-29 15:39:25 +08:00
    @iscraft ... 吓我一跳,你以为搞前端呢,/ 在 php 服务器端表示服务器上的根目录, 网页上你可以用 / 访问网站目录下的资源,服务端不可以。
    iscraft
        21
    iscraft  
    OP
       2016-12-29 15:45:35 +08:00
    @Felldeadbird 写法是不对 但在 windows 的同一个 apache2.4.7 下分别加载了 5.2/5.4/5.6/7.0.12 针对 /符号 7 出现了错误 就想搞明白这个是为啥
    greatonce
        22
    greatonce  
       2016-12-29 15:50:37 +08:00
    @iscraft

    Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.

    http://php.net/manual/zh/ini.core.php#ini.include-path
    greatonce
        23
    greatonce  
       2016-12-29 15:52:27 +08:00
    @iscraft 因为你的 include_path 配置 指定了当前目录可以被当做搜索目录, php7 的配置可能没有这个而已

    你可以看看 几个不同版本的 php.ini 里面的 include_path 选项。
    iscraft
        24
    iscraft  
    OP
       2016-12-29 16:07:45 +08:00
    @greatonce php.ini 中 include_path 选项均未打开
    greatonce
        25
    greatonce  
       2016-12-29 17:34:36 +08:00
    @iscraft 你在二楼不是回复了吗,没打开不可能有你在二楼说的那个设置,你看的可能是 php.ini 的模板,而不是真正的配置文件。

    总之,这种写法肯定不对,网站 url 和系统路径不是一回事。
    lslqtz
        26
    lslqtz  
       2016-12-29 17:38:42 +08:00
    使用相对路径不好吗?
    royantar
        27
    royantar  
       2016-12-29 17:51:09 +08:00
    7.0 版本将 Windows 环境下这样的路径视为绝对路径:

    #if PHP_WIN32
    /* This should count as an absolute local path as well, however
    IS_ABSOLUTE_PATH doesn't care about this path form till now. It
    might be a big thing to extend, thus just a local handling for
    now. */
    filename_length >=2 && IS_SLASH(filename[0]) && !IS_SLASH(filename[1]) ||
    #endif
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5333 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 08:06 · PVG 16:06 · LAX 01:06 · JFK 04:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.