PHP 包含文件的路径

发布于 2024-12-06 06:41:51 字数 476 浏览 0 评论 0 原文

我有一个带有index.php 的网站。 index.php 有许多包含文件,例如

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

我在根目录下创建了一个名为“extrapages”的新文件夹,并且我将在其中包含包含与该站点相关的信息的页面。我已经添加了像上面这样的 include_once 文件并更改了路径,这些连接正常。

我发现的问题是包含文件中的路径失败。例如:如果图像或另一个包含文件位于包含文件中,则从“extrapages”文件夹运行时会失败。路径问题。

有没有好的办法来处理这个问题呢? 我是否可以偶然将“extrapages”下页面的站点根目录 (www) 路径更改/设置为一个文件夹?

我可以将这些页面移到根目录上,它们会运行良好,但我真的不希望网站的根目录上出现所有混乱。

任何想法&谢谢

I have a site with an index.php.
The index.php has a number of include files like

 <?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. I've added the include_once files like above and change the path and these hook up fine.

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. 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事和酒 2024-12-13 06:41:51

任何路径问题的关键都称为绝对路径

  • 在为您的网站(包括图像源)创建超链接时,始终从/ 随后是完整的正确路径。它永远不会让您失望。

  • 文件系统调用相同:始终使用绝对路径。您的服务器通常会为您提供一个非常方便的变量,名为 $_SERVER['DOCUMENT_ROOT'] ,其中包含文件系统与 Web 服务器的交汇点,指向您的 Web 根目录。

因此,当从站点中的任何位置调用时,

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

将始终指向同一位置

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.

So, when called from anywhere in your site,

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

will point always to the same location

金橙橙 2024-12-13 06:41:51

如果您使用的是 PHP 5.3.0 或更高版本,您可以(而不是 RageZ)建议的,仅使用 __DIR__ (新定义的魔术常量)。

例如:

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

现在,当您想避免 ../ 并映射您的包含时,这没有帮助。不过,有一种更好的方法来做到这一点 - 在所有前端文件(应该是唯一用户可访问的 PHP 文件)中,您定义一个常量,该常量提供脚本的根路径(而不是当前文件的根路径)。

例如:

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';

现在假设我们要在包含目录中包含一个文件。

让我们在 includes/myinclude.php 中执行此操作,并包含文件 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';

请记住包含路径应​​该直接相对于项目本身的 root 目录,而不仅仅是前端文件之一。如果子目录中有前端文件,则在定义常量时需要返回到项目根目录本身。

例如:

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';

我们在这里所做的就是添加一个 dirname() 调用,该调用会删除路径中的目录。
请参阅: http://us.php.net/manual/en/function.dirname .php

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).

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).

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.

Let's do it in includes/myinclude.php, and include the file 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.
See: http://us.php.net/manual/en/function.dirname.php

温柔戏命师 2024-12-13 06:41:51

只需使用 set_include_path() 添加包含路径一次(在开头或配置文件中的某个位置),请参阅 手册。使用绝对路径(不是相对路径;可以利用dirname(__FILE__))并且它应该始终有效。

Just add your include path once (somewhere at the beginning or in a config file) using set_include_path(), see the manual. Use an absolute path (not relative; can utilize dirname(__FILE__)) and it should work all the time.

橘亓 2024-12-13 06:41:51

您应该使用 dirname__FILE__ 通过使用这两个常量,您应该能够包含相对于当前文件而不是 Web 服务器调用的 php 脚本。

例如

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

dirname:将返回路径的目录部分

__FILE__:是一个魔术常量,它被当前文件的路径替换。

这样做的唯一问题是你锁定了项目的结构,但大多数时候这是可以接受的。

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.

for example

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

dirname: would return the directory part of a path

__FILE__: is a magic constant, it's replaced by the path of the current file.

The only problem with doing such thing you lock the structure of your project but most of the times it's acceptable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文