简体   繁体   English

PHP包含文件的路径

[英]PHP Path to Include Files

I have a site with an index.php. 我有一个带有index.php的网站。 The index.php has a number of include files like index.php有许多包含文件,例如

 <?php include_once('scripts/php/index_mainImageJoin.php');?>

I've created a new folder off the root called 'extrapages' and I'm going to have pages in there that have information relating to the site. 我从根目录创建了一个名为“ extrapages”的新文件夹,并将在其中具有包含与该站点有关的信息的页面。 I've added the include_once files like above and change the path and these hook up fine. 我已经像上面一样添加了include_once文件,并更改了路径,这些都很好。

The problem I'm find is paths within the include files fail. 我发现的问题是包含文件中的路径失败。 eg: if an image or another include file is within the include it fails when run from the 'extrapages' folder. 例如:如果图像或其他包含文件在包含文件中,则从“ extrapages”文件夹运行时,它将失败。 Pathing issue. 路径问题。

Is there a good way to deal with this? 有解决这个问题的好方法吗? Can I change/set the path to the site root (www) for pages under 'extrapages' to one folder down by chance? 是否可以将“扩展名”下页面的站点根目录(www)的路径更改/设置为一个文件夹?

I could move these pages onto the root and they would run fine but I really don't want all the clutter on the root of the site. 我可以将这些页面移到根目录上,并且它们可以正常运行,但我真的不希望网站根目录上的所有内容杂乱无章。

Any ideas & thx 任何想法和想法

The key to any path problem is called absolute path 解决任何路径问题的关键称为绝对路径

  • while creating hyperlinks for your site (including image sources), always start it from / followed by full correct path. 在为您的网站(包括图像源)创建超链接时 ,请始终从/后面加上完整的正确路径启动它。 And it never fail you. 它永远不会让你失望。

  • same for the filesystem calls: always use absolute path. 与文件系统调用相同:始终使用绝对路径。 Your server usually provides you with very handy variable called $_SERVER['DOCUMENT_ROOT'] contains the point where filesystem meet web-server, pointing to your web root directory. 您的服务器通常为您提供一个非常方便的变量,称为$_SERVER['DOCUMENT_ROOT']该变量包含文件系统与Web服务器的连接点,指向您的Web根目录。

So, when called from anywhere in your site, 因此,当您从网站上的任何地方致电时,

include $_SERVER['DOCUMENT_ROOT'].'/scripts/php/index_mainImageJoin.php';

will point always to the same location 将始终指向同一位置

you should use dirname and the __FILE__ by using this both constant you should be able to include file relative to the current file instead of the php script called by the web server. 您应该使用dirname__FILE__通过使用这两个常量,您应该能够包含相对于当前文件的文件,而不是包含Web服务器调用的php脚本。

for example 例如

include_once dirname(__FILE__) . '/../include.php';

dirname : would return the directory part of a path dirname :将返回路径的目录部分

__FILE__ : is a magic constant, it's replaced by the path of the current file. __FILE__ :是一个魔术常数,它被当前文件的路径替换。

The only problem with doing such thing you lock the structure of your project but most of the times it's acceptable. 这样做的唯一问题是锁定了项目的结构,但是大多数情况下它是可以接受的。

Just add your include path once (somewhere at the beginning or in a config file) using set_include_path() , see the manual . 只需使用set_include_path()将包含路径添加一次(在开头的某个位置或在配置文件中set_include_path() ,请参阅手册 Use an absolute path (not relative; can utilize dirname(__FILE__) ) and it should work all the time. 使用绝对路径(不是相对路径;可以使用dirname(__FILE__) ),它应该一直工作。

If you're on PHP 5.3.0 or newer, you can (instead of what RageZ) suggested, use just __DIR__ (a newly defined magic constant). 如果您使用的是PHP 5.3.0或更高版本,则可以(而不是__DIR__建议)使用__DIR__ (新定义的魔术常数)。

example: 例:

include __DIR__ . '/../include.php';

Now, this doesn't help when you want to avoid ../ and mapping out your includes. 现在,当您要避免使用../并映射包含项时,这无济于事。 There's a better way to do it, though - in all front-end files (which should be the ONLY user-accessible PHP files) you define a constant which provides the root path of your script (and not of the current file). 不过,还有一种更好的方法-在所有前端文件(应该是用户只能访问的PHP文件)中,定义一个常量,该常量提供脚本(而不是当前文件)的根路径。

For example: 例如:

index.php

<?php
define('MYSCRIPT_ROOT', dirname(__FILE__));
// or in php 5.3+ ...
define('MYSCRIPT_ROOT', __DIR__);

// ... do some stuff here

include MYSCRIPT_ROOT . '/includes/myinclude.php';

Now let's say we want to include a file in our includes directory. 现在,我们要在includes目录中包含一个文件。

Let's do it in includes/myinclude.php , and include the file includes/myotherinclude.php 让我们在includes/myinclude.php ,并包含include includes/myotherinclude.php文件。

includes/myinclude.php

<?php
if(!defined('MYSCRIPT_ROOT')) exit; // prevent direct access - ALWAYS a good idea.

// ... do stuff here or something

include MYSCRIPT_ROOT . '/includes/myotherinclude.php';

Keep in mind that the include paths should be directly relative to the root directory of the project itself, not to just one of the front-end files. 请记住,包含路径应​​直接相对于项目本身的目录,而不仅仅是相对于前端文件之一。 If you have a front-end file in a subdirectory, you need to back out to the project root itself when defining the constant. 如果子目录中有一个前端文件,则在定义常量时需要回退到项目根目录本身。

example: 例:

subdirectory/index.php

index.php

<?php
define('MYSCRIPT_ROOT', dirname(dirname(__FILE__)));
// or in php 5.3+ ...
define('MYSCRIPT_ROOT', dirname(__DIR__));

// ... do some stuff here

include MYSCRIPT_ROOT . '/includes/myinclude.php';

All we do here is add a dirname() call, which takes off a directory in the path. 我们在这里所做的只是添加一个dirname()调用,该调用将删除路径中的目录。 See: http://us.php.net/manual/en/function.dirname.php 请参阅: http//us.php.net/manual/en/function.dirname.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM