简体   繁体   English

php include,所有路径搞砸了

[英]php include, all paths get messed up

When i use php include to include a page in my website all the paths in the file i include get messed up. 当我使用php include在我的网站中包含一个页面时,我所包含的文件中的所有路径都搞砸了。 The included page acts like it is in the same folder as the page im including from. 包含的页面就像它与页面im包含的文件夹在同一文件夹中。

Is there way to avoid/fix this problem? 有没有办法避免/解决这个问题?

One way I try to get around this problem is by always including from where the file that is including the other file is based: 我尝试解决此问题的一种方法是始终包含包含其他文件的文件所在的位置:

$here = dirname(__FILE__);
include($here."/../include.php"); 
// will include a file *allways* one level up from where *this* file is located
// and not the file that started the execution of the script.

I sometimes have files that are accessed from several different places and so the includes file path can become a bit hard to manage. 我有时会从几个不同的地方访问文件,因此包含文件路径可能会变得有点难以管理。 So I usually try to include a configuration file at a known point then define paths to common include points. 所以我通常尝试在已知点包含配置文件,然后定义公共包含点的路径。

// from a common config file
define("PATH_TO_CLASS", dirname(__FILE__)."/../class");
define("PATH_TO_MEDIA", dirname(__FILE__)."/../assets/media");

Then you can use in the file you've included the config file like: 然后你可以在包含配置文件的文件中使用如下:

include dirname(__FILE__)."/../config.php";
include PATH_TO_CLASS."/snassy.class.php";

You might need to set the include path correctly, for example via: 您可能需要正确设置包含路径,例如通过:

set_include_path(get_include_path() . PATH_SEPARATOR . 'YourPath');

Then you just need to include as if it were is the same directory: 然后你只需要包括好像它是同一个目录:

include 'FileName.php';

What is your include path set to? 您的包含路径设置为什么? If included.php is not the same directory as page.html, you can append to you existing include path. 如果included.php与page.html不是同一目录,则可以向您追加现有的包含路径。

<?php
$path = '/path/to/includes';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
?>

Try the PHP manual 试试PHP手册

No, that is the default behaviour of the php include function: It basically copies the contents of the included file into the including file. 不,这是php include函数的默认行为:它基本上将包含文件的内容复制到包含文件中。 That way the instructions in the included file behave as if they were in the including file (because they are in a way). 这样,包含文件中的指令就像它们在包含文件中一样(因为它们在某种程度上)。

You either have to refactor the instructions in you included file so they can operate from the folder of the including file, or add the folder of the included file to the include path. 您必须重构包含文件中的指令,以便它们可以从包含文件的文件夹操作,或者将包含文件的文件夹添加到包含路径。

You could use absolute paths? 你可以使用绝对路径? ' http://www.site.com/the/full/url/image.jpg ' ' http://www.site.com/the/full/url/image.jpg '

Or even 甚至

$path = 'http://www.site.com';

and $path.'images/image.jpg' or whatever it is $path.'images/image.jpg'或者不管它是什么

Another answer for you. 另一个答案给你。 If things are unchangeable for some reason I've used chdir() to some success. 如果由于某种原因事情是不可改变的, 我已经使用chdir()取得了一些成功。

For example I had mobile users come from and use a different directory than the main site, but wanted to use all of the functions and classes of the main site, so I would chdir(/to/the/main/site/folder); 例如,我有移动用户来自并使用与主站点不同的目录,但是想要使用主站点的所有功能和类,所以我想chdir(/to/the/main/site/folder); right before calling the common code, which required different include paths. 在调用公共代码之前,需要不同的包含路径。 I would use sparingly though... as things can get confusing. 我会谨慎使用......因为事情会让人感到困惑。

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

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