代码之家  ›  专栏  ›  技术社区  ›  Squirkle

通过变量包含文件

  •  0
  • Squirkle  · 技术社区  · 15 年前

    这似乎是世界上最基本的东西,但我不懂PHP。

    我试图在我的页面中包含一个文件,并且该文件的目录是在一个变量中指定的。

    <?php
        $pageGroup = 'news';
        $directory = 'http://localhost:8888/includes/'.$pageGroup.'/rightCol.html';
        include($directory);
    ?>
    

    当我 echo 结果 $directory ,我明白了“ http://localhost:8888/includes/news/rightCol.html

    2 回复  |  直到 15 年前
        1
  •  0
  •   Sarfraz    15 年前

    确保你有 allow_url_fopen

    尽管如此,也存在安全风险,见:

    建议:

    要包含文件,不要使用完整的url路径,只使用文件/文件夹路径。

    例子:

    include "/includes/news/rightCol.html"; // get from specified dir
    include "../rightCol.html"; // get from parent dir
    include "../../rightCol.html"; // get from parent parent dir
    

    要包含根目录中的文件,您可能需要在路径前面加上以下前缀:

    $_SERVER['DOCUMENT_ROOT'];
    
        2
  •  0
  •   fabrik    15 年前

    出于安全考虑,您不希望包含http://resource。而不是你需要通过 file_get_contents 但是如果这是一个简单的页面元素,您可以使用include而不需要完整的url。