【发布时间】:2022-01-05 15:10:21
【问题描述】:
我正在尝试用 php 创建一个文件管理器,所以当我在浏览器中打开它时,它会给出当前目录的列表,并且可以使用 html 中的锚标记单击该文件(到目前为止我已经这样做了) ,当文件被点击时,它会以文本模式打开它并显示文件中的任何源代码。
我面临两个我无法解决的问题
问题 #1:
第一个问题是我希望我的文件管理器能够读取任何源代码,无论它是图像还是 pdf,就像我发现的 tinyfilemanager here 这个杰作可以读取任何文件,即使你用一个记事本并在文件的最后插入一些 php 代码,它也会读取它,所以这是我的源代码:
<?php
function list_all_files($directory){
//opening the dir
if($handle=opendir($directory.'/')){
echo "looking inside '$directory'"."<br>";
}
while($file=readdir($handle)){
//listing all the directories without ".." (dots)
if($file!='.'&&$file!='..') {
echo '<a href="Read.php?dir='.$directory.'&read='.$directory.'/'.$file.'">'.$file.'</a><br>';
} //if ends here
} //while loop endds here
} //list_all_files ends here
function read_file($file)
{
$handle = fopen($file, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo($line);
}
fclose($handle);
} else {
echo "error opening the file";
}
}
//main function
if(!isset($_GET['dir'])) {
$dir='images';
}else{
$dir=$_GET['dir'];
}
list_all_files($dir);
if(isset($_GET['read'])){
$file1 = $_GET['read'];
read_file($file1);
}
?>
我制作的上述程序也可以读取文件代码,但是当我点击任何包含 html 代码的 PHP 文件时,它只是显示它而不是以文本模式提供其源代码,如下图:
不仅如此,如果我使用记事本将一些 php 代码放在图像文件的最后,它就不会显示它。检查这个:
我做了很多研究,为什么我的代码无法正常工作,而 tinyFilemanager 与上述任何一种情况都很完美,我发现每当我通过浏览器执行页面文件时,它默认使用这个
header("Content-Type: text/html");
所以如果我想做我想做的事,那么我将不得不使用这个:
header("Content-Type: text/x-php");
涵盖了上述两种情况,但会导致第二个问题。
问题 #2:
<?php
function list_all_files($directory){
//opening the dir
if($handle=opendir($directory.'/')){
echo "looking inside '$directory'"."<br>";
}
while($file=readdir($handle)){
//listing all the directories without ".." (dots)
if($file!='.'&&$file!='..') {
echo '<a href="Read.php?dir='.$directory.'&read='.$directory.'/'.$file.'">'.$file.'</a><br>';
} //if ends here
} //while loop endds here
} //list_all_files ends here
function read_file($file)
{
$handle = fopen($file, "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
echo($line);
}
fclose($handle);
} else {
echo "error opening the file";
}
}
//main function
if(!isset($_GET['dir'])) {
$dir=getcwd();
}else{
$dir=$_GET['dir'];
}
//listing all the directories and files in text/html format so that our anchor tag would be available.
ob_start();
header('Content-Type: text/html; charset=UTF-8');
list_all_files($dir);
ob_end_flush();
if(isset($_GET['read'])){
//changing the header to text/php-x so that the php code in any jpg file can be viewed clearly
ob_clean();
header('Content-Type: text/x-php; charset=UTF-8');
ob_start();
$file1 = $_GET['read'];
read_file($file1);
ob_end_flush();
}
?>
上面的代码工作得很好,但是有一个问题。由于它的内容类型不再是 text/html,它不会在网页上显示 html 内容。这既好又坏,因为那时我不会以锚标记形式获得目录列表,因为我认为 ob_start 和 ob_end_flush()。如果我使用这两个,它只会通过为每个函数分别创建一个缓冲区并执行它来解决问题。因此,当它执行时,上面的函数将使用内容类型 text/html 呈现,并显示带有锚标记的目录列表,第二个将只是在 text/x-php 中,这将解决上述两种情况,但是我错了。
【问题讨论】:
-
请更专注于您的问题
-
我必须同意 Ken Lee 的观点,目前还不清楚您到底想要什么。尝试专注于一个问题。
-
查看框架。一帧可以是html,另一帧可以是文本。或者将 read_file 的输出包装在 pre 标记中,然后 html 对其进行转义。您显示的特定文件在十年前被 WHCM 确定为攻击,我还看到了一个名为
deface.php的文件,所以我担心它会被如何使用。 -
你可能把一些非常简单的东西弄得太复杂了。 PHP 在服务器上运行。 PHP 的输出从服务器传输到客户端。在客户端,来自 PHP 的输入由浏览器解释。你可能很难理解后者。浏览器不会被动地呈现其输入。如果您想查看原始输入,您必须查看the source code。
-
你还是没有道理。你在说这个show_source()吗?无论如何,我很高兴你解决了你的问题。
标签: php file fopen file-manager