PHP 无法将包含 PHP 代码的文件读取为文本文件

发布于 2024-07-15 00:32:01 字数 324 浏览 4 评论 0原文

我偶然发现了以下特殊之处:

$handle = fopen(realpath("../folder/files.php"), "r");

无法读取文件,但是一旦我从文件中删除 php 标签, 它变得可读,并且我的脚本在页面上打印非空文件内容。 另外,file.php 永远不会被执行,所以我想知道为什么这是问题所在。

我猜想 Apache 或 PHP 不允许以文本方式读取包含 php 标签 PHP 的文件。

如何为我的特定文件启用它(当然在全球范围内这样做是不安全的)?

使用 PHP 5.2.x 和 Apache 2.0

I've stumbled upon the following pecularity:

$handle = fopen(realpath("../folder/files.php"), "r");

can't read a file, but as soon as I remove php tags from the file,
it becomes readable and my scripts prints non-empty file content on the page.
Also, file.php is never ever executed, so I wonder why it is the problem.

I guess somehow Apache or PHP doesn't let read files containing php tags PHP as just text.

How can I enable it for my particular file (course doing it globally would be unsecure)?

Using PHP 5.2.x and Apache 2.0

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

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

发布评论

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

评论(6

夏末的微笑 2024-07-22 00:32:14

重命名文件
阅读
获取你的数据
改变他们
重命名你的文件:)

rename the file
read it
get your data
change them
rerename your file :)

掩饰不了的爱 2024-07-22 00:32:13

我正在使用 htmlspecialchars($line);

它解决了问题。

I am using htmlspecialchars($line);

It solved the problem.

贱贱哒 2024-07-22 00:32:11

Wadih M. 解决了这个问题。 PHP 标签在浏览器中隐藏整个标签封闭的 php 源输出,但“查看源代码”显示它们。 谢谢!

Wadih M. solved the issue. PHP tags hides the whole tag-enclosed php source output in the browser, but View Source shows them. Thanks!

温柔嚣张 2024-07-22 00:32:09

嗯 file_get_contents() 对我有用。 只有fopen有这个问题吗?

编辑:你到底得到了什么错误?

Hmm file_get_contents() is working for me. Is it issue only for fopen?

Edit: what error do you get exactly?

怪我闹别瞎闹 2024-07-22 00:32:06

您确定正确解释了输出吗? 如果将文件内容直接打印到页面输出,浏览器将不会显示尖括号内的文本,因为它认为它是标签。 以 text/plain 形式提供您的内容,或使用浏览器的“查看源代码”命令来确保您查看的是实际内容,而不仅仅是浏览器选择显示的内容。

Are you sure you're interpreting the output correctly? If you print the file contents directly to your page output, a browser won't display text inside angle brackets because it thinks it's a tag. Serve your content as text/plain, or use your browser's "view source" command to make sure you're looking at what's really there, and not just what the browser chose to display.

我要还你自由 2024-07-22 00:32:05

我得到了它。 我正在使用 Google Chrome 调试页面,我意识到在查看源代码时,Chrome 由于某种原因隐藏了 PHP 标签。 我在 Firefox 上进行了相同的测试,查看源代码证明一切正常。

以下是测试详细信息:

代码:

$fh = fopen("test.php","r");
while ($line = fgets($fh)){
echo $line;
}

要读取的文件(test.php):

testing <?php testing2 ?> testing3

渲染(在 Chrome 和 Firefox 上):

testing  testing3

查看源代码(使用 Firefox):

testing <?php testing2 ?> testing3

查看源代码(使用 Chrome - 我的错误来源):

testing  testing3

I got it. I was using Google chrome to debug the page, and I realized that when viewing the source, Chrome hides PHP tags for some reason. I ran the same test on Firefox, and viewing the source proved that everything was okay.

Here are the test details:

Code:

$fh = fopen("test.php","r");
while ($line = fgets($fh)){
echo $line;
}

File to be read (test.php):

testing <?php testing2 ?> testing3

Rendering (on both Chrome and firefox):

testing  testing3

View source (using firefox):

testing <?php testing2 ?> testing3

View source (using Chrome - source of my mistake):

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