donk68254 2012-08-09 11:57
浏览 80
已采纳

如何为PHP包含文件设置根文件夹

I've spent days researching the internet and I can't find the answer that I will understand and are able to implement.

I've got the website in which I want to use include_once files and be able to view the website properly in both localhost and the actual server.

I've used $_SERVER['DOCUMENT_ROOT'] before but it doesn't work on the localhost so it is hard for me to update and do the changes to the website.

What I want to do is to create some sort of config.php file in which I can tell it that the root folder is either called htdocs or public_html and to "start" looking for files from there.

Or is there any other way I can get include paths to work?

I've seen replies with e.g. dirname(__FILE__), __DIR__, etc. but they never explained how it would work, where to write it in the code, etc. I'm a bit sick and tired of finding part of the answer and then running around in circles just try and "fill in" the gaps. I'm not a PHP expert, just trying to make my life a bit easier by using it.

  • 写回答

5条回答 默认 最新

  • dousu1916 2012-08-09 12:42
    关注

    dirname(__FILE__) and __DIR__ both same and __DIR__ comes with PHP 5.3

    These are used in order to indicate that the "path of the file where they called".

    URL: http://localhost/test/a.php
    
    DIR: --NIX
         /var/www/test/a.php
         --WIN
         D:\lamp\www\test\a.php
    
    // a.php's inside
    &lt?php
    echo __DIR__;
    

    Gives you this on linux: /var/www/test

    So, if you need a config parameter in all your project, just define it in your config.php and use it where you want both the file name that will be included.

    ./
      config.php
      index.php
      header.php
      footer.php
      /lib
        foo.php
      /tmp
        bar.php
    

    ./config.php define('ROOT', __DIR__ .'/');

    ./index.php include_once(ROOT .'header.php'); ... include_once(ROOT .'footer.php');

    i.e, using it in tmp dir

    ./tmp/bar.php include_once(ROOT .'lib/foo.php');

    UPDATE

    // config.php
    <?php
    define("ROOT", __DIR__ ."/");
    

    So, we use this for index.php to include banner.php and banner.php is waiting in ./banners/banner.php;

    // index.php and the very first line!
    <?php
    include_once("config.php");
    ?>
    // some html stuff
    // ...
    <?php include_once(ROOT ."banners/banner.php"); ?>
    // some more html stuff
    // ...
    

    So, you should include config.php first to where you need it.

    I think, this is basic as far as needed...

    UPDATE

    So your problem is not PHP include system, but question, anyway... :)

    If your image path is changing (so not fixed), you can do like this;

    // config.php
    define("ROOT", __DIR__ ."/");
    define("HTTP", ($_SERVER["SERVER_NAME"] == "localhost")
       ? "http://localhost/your_work_folder/"
       : "http://your_site_name.com/"
    );
    
    // banner.php
    <img src="<?php print HTTP; ?>images/banner.gif">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器