PHP [function.require]:无法打开流:没有这样的文件或目录问题

发布于 2024-09-17 01:25:01 字数 384 浏览 2 评论 0原文

我的一个函数包含以下错误。

require(../htmlpurifier/library/HTMLPurifier.auto.php) [function.require]: failed to open stream: No such file or directory

如果我对路径进行编码,我可以纠正这个问题

./htmlpurifier/library/HTMLPurifier.auto.php

,但是我想在我的网站上的许多不同级别的文件夹中显示包含文件,而不必每次都在我的函数包含内重新编码路径,这违背了我的包含的目的。有没有一种方法可以让我不必每次都重新编码路径来完成这项工作?有没有办法包含完整路径?

I get the following error in one of my function includes.

require(../htmlpurifier/library/HTMLPurifier.auto.php) [function.require]: failed to open stream: No such file or directory

I can correct this problem if I code the path

./htmlpurifier/library/HTMLPurifier.auto.php

But I want to display the include file in many different levels of folders on my website without having to re-code the path inside my function include every time which defeats the purpose of my include. Is there a way I can have this work with out having to re-code the path every time? Is there a way to include the full path?

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

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

发布评论

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

评论(4

夏见 2024-09-24 01:25:01

试试这个:

require realpath(dirname(__FILE__) . '/htmlpurifier/library/HTMLPurifier.auto.php');

这样你总是包含相对于当前文件路径的文件。

Try this:

require realpath(dirname(__FILE__) . '/htmlpurifier/library/HTMLPurifier.auto.php');

That way you always include files relative to the path of the current file.

☆獨立☆ 2024-09-24 01:25:01

您使用的路径错误。
只需将这两条线放在一起即可感受差异。

../htmlpurifier/library/HTMLPurifier.auto.php
./htmlpurifier/library/HTMLPurifier.auto.php

这是两条不同的路径,永远不会同时起作用。后一个指向当前目录,而第一个指向更高一级。

但我想在我的网站上的许多不同级别的文件夹中显示包含文件,而不必重新编码函数内的路径

,这是明智的愿望。给你:(

require $_SERVER['DOCUMENT_ROOT'].'/htmlpurifier/library/HTMLPurifier.auto.php';

假设这个 htmlpurifier 文件夹位于文档根目录中)

You are using wrong path.
Just put these 2 lines together and feel the difference.

../htmlpurifier/library/HTMLPurifier.auto.php
./htmlpurifier/library/HTMLPurifier.auto.php

That's 2 different paths and will never work at the same time. Latter one points to the current directory while first one - one level higher.

But I want to display the include file in many different levels of folders on my website without having to re-code the path inside my function

that's sensible desire. Here you go:

require $_SERVER['DOCUMENT_ROOT'].'/htmlpurifier/library/HTMLPurifier.auto.php';

(assuming this htmlpurifier folder lays in the document root)

为人所爱 2024-09-24 01:25:01

试试这个

require (dirname(__FILE__).'/../htmlpurifier/library/HTMLPurifier.auto.php');

Try this

require (dirname(__FILE__).'/../htmlpurifier/library/HTMLPurifier.auto.php');
过潦 2024-09-24 01:25:01

如何将此 .php 文件的路径配置为脚本中的配置变量?假设您将在所有页面中加载一些通用函数库,因此只需执行以下操作:

$PATH_TO_PURIFIER = 'C:\this\that\whatever\HTMLPurifier.auto.php';

在该通用文件中,然后您就可以执行

include('commonstuff.php');
include($PATH_TO_PURIFIER);

只要 commonstuff.php 位于 PHP 的 include_path 中的某个位置,那么这样做会更可靠方式而不是尝试在其他地方动态计算路径。特别是如果您的目录结构可能发生变化,导致计算出的路径无效并迫使您再次更改每个 PHP 文件。

How about configuring the path to this .php file as a configuration variable in your script? Presumably you'll be loading some common functions library in all your pages, so just do:

$PATH_TO_PURIFIER = 'C:\this\that\whatever\HTMLPurifier.auto.php';

in that common file, then you can just do

include('commonstuff.php');
include($PATH_TO_PURIFIER);

As long as commonstuff.php is somewhere in your PHP's include_path, it's far more reliable to do it this way rather than try to dynamically calculate the path everywhere else. Especially if your directory structure might change, rendering the calculated path invalid and forcing you to change every PHP file yet again.

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