PHP无法将包含PHP代码的文件读取为文本文件 [英] PHP can't read files containing PHP code as text files
问题描述
我偶然发现了以下几种现象:
I've stumbled upon the following pecularity:
$ handle = fopen(realpath("../folder/files.php"),"r");
$handle = fopen(realpath("../folder/files.php"), "r");
无法读取文件,但是一旦我从文件中删除了php标签, 它变得可读,并且我的脚本在页面上打印非空文件内容. 另外,file.php从未执行过,所以我想知道为什么是这个问题.
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.
我猜想Apache或PHP不允许以文本形式读取包含php标签PHP的文件.
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)?
使用PHP 5.2.x和Apache 2.0
Using PHP 5.2.x and Apache 2.0
推荐答案
我明白了.我当时使用Google chrome调试页面,并且意识到在查看源代码时,Chrome出于某些原因会隐藏PHP标签.我在Firefox上进行了相同的测试,查看源代码证明一切正常.
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.
以下是测试详细信息:
代码:
$fh = fopen("test.php","r");
while ($line = fgets($fh)){
echo $line;
}
要读取的文件(test.php):
File to be read (test.php):
testing <?php testing2 ?> testing3
渲染(在Chrome和firefox上):
Rendering (on both Chrome and firefox):
testing testing3
查看源代码(使用Firefox)
View source (using firefox):
testing <?php testing2 ?> testing3
查看源代码(使用Chrome-我的错误来源):
View source (using Chrome - source of my mistake):
testing testing3
这篇关于PHP无法将包含PHP代码的文件读取为文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!