【问题标题】:Getting file in subfolder of same directory在同一目录的子文件夹中获取文件
【发布时间】:2016-07-20 14:33:08
【问题描述】:

我有这样的结构:

currentFolder
    currentFile.php
    email
        email.php

我在文件currentFile.php 中,需要包含email.php 文件。

我尝试过包含email/email.php/email/email.php,但都不起作用?

将此email.php 包含到currentFile.php 中的实际网址是什么?

【问题讨论】:

    标签: php html directory-structure subdirectory


    【解决方案1】:

    您需要用单引号或双引号将字符串括起来。像这样:

    include 'email/email.php';
    

    如果您的字符串以/ 字符开头,PHP 将仅从文件系统的根目录查找。 (而不是相对于您当前的文件)

    所以include '/email/email.php'; 会看这里:

    /email/email.php
    

    include 'email/email.php'; 会看这里:

    /currentFolder/email/email.php
    

    【讨论】:

    • 对不起,我确实将它们包装在一个字符串中,但是当我回显该包含的内容时,它不起作用。例如:$test = 包括 'email/email.php';然后我会 print_r($test);死();它是空的? Email.php 文件中包含 html 和 PHP。
    • 这超出了这个问题的范围。但简单地说,你不能回显某些东西,它会由文件返回。 (回显总是打印到页面上)但我会先从上到下阅读this page,然后查看W3 Schools PHP 教程。
    • 另外,die()exit() 将杀死所有当前正在运行的 PHP。这适用于您从中包含的文件。
    【解决方案2】:

    使用常量DIR可以知道当前文件所在的目录。

    包括 DIR.'/email/email.php

    【讨论】:

      【解决方案3】:

      在这种情况下最好使用__DIR__。试试这个:

      require_once __DIR__ . "\email\email.php"
      

      但仅适用于 PHP 5.3

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-07-02
        • 2012-07-31
        • 2013-08-20
        • 1970-01-01
        • 1970-01-01
        • 2013-02-28
        • 2012-07-23
        • 1970-01-01
        相关资源
        最近更新 更多