如何为 PHP 包含文件设置根文件夹 [英] How to set root folder for PHP include files

查看:37
本文介绍了如何为 PHP 包含文件设置根文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几天时间研究互联网,但找不到我能理解并能够实施的答案.

我有一个网站,我想在其中使用 include_once 文件,并且能够在 localhost 和实际服务器中正确查看该网站.

我以前使用过 $_SERVER['DOCUMENT_ROOT'] 但它在本地主机上不起作用,因此我很难更新和更改网站.

我想做的是创建某种 config.php 文件,我可以在其中告诉它根文件夹名为 htdocspublic_html 并开始"从那里寻找文件.

或者还有其他方法可以让包含路径起作用吗?

我看到过回复,例如dirname(__FILE__), __DIR__ 等等,但他们从来没有解释过它是如何工作的,在代码中的哪里写等等.我有点恶心和疲倦找到答案的一部分,然后绕圈跑,只是尝试填补"空白.我不是 PHP 专家,只是想通过使用它让我的生活更轻松.

解决方案

dirname(__FILE__)__DIR__都一样,__DIR__自带PHP 5.3

这些用于指示他们调用的文件的路径".

<上一页>网址:http://localhost/test/a.php目录:--NIX/var/www/test/a.php- 赢D:灯www esta.php//a.php 在里面<?php回声 __DIR__;

在 linux 上给你这个:/var/www/test

因此,如果您在所有项目中都需要一个配置参数,只需在您的 config.php 中定义它并在您想要包含的文件名的地方使用它.

<上一页>./配置文件索引.php头文件.php页脚.php/libfoo.php/tmp酒吧.php

./config.phpdefine('ROOT', __DIR__ .'/');

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

即在 tmp dir

中使用它

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

更新

//config.php

所以,我们使用 index.php 来包含 banner.php 并且 banner.php 中等待./横幅/banner.php;

//index.php 和第一行!

因此,您应该首先将 config.php 包含在您需要的位置.

我认为,就需要而言,这是基本的......

更新

所以你的问题不是 PHP include 系统,而是问题,反正... :)

如果你的图片路径在变化(所以不固定),你可以这样做;

//config.php定义(根",__DIR__ ./");定义("HTTP", ($_SERVER["SERVER_NAME"] == "localhost")?http://localhost/your_work_folder/":http://your_site_name.com/");//横幅.php<img src="<?php 打印 HTTP;?>images/banner.gif">

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.

解决方案

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:lampwww	esta.php

// a.php's inside
<?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">

这篇关于如何为 PHP 包含文件设置根文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆