简体   繁体   English

使用 php fopen 读取文件和制作文件管理器时出现问题

[英]Problem in reading files with php fopen and in making file manager

I am trying to make a file manager with php, so when I open it in browser it would give a list of the current directory and the file would be clickable using the anchor tag in html (which I have done so far), and when the file is clicked, it would open it in the text mode and shows whatever the source code inside the file is.我正在尝试使用 php 制作一个文件管理器,所以当我在浏览器中打开它时,它会给出当前目录的列表,并且可以使用 html 中的锚标记来点击该文件(到目前为止我已经这样做了),当单击该文件,它将以文本模式打开它并显示文件中的任何源代码。

I am facing two problems which I couldn't figure out我面临两个我无法弄清楚的问题

Problem #1:问题 1:

The first problem is that I want my file manager to read any source code weather its an image or pdf, just like the tinyfilemanager that I found here this master piece can read any file, even if you open an image with a notepad and insert some php code at the very end of the file it will read render that too, so here's my source code:第一个问题是我希望我的文件管理器能够读取任何源代码,无论它是图像还是 pdf,就像我在这里找到的 tinyfilemanager 一样,这个杰作可以读取任何文件,即使你用记事本打开图像并插入一些文件末尾的 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);
}

?>

the above program I made can also read files code but when I click on any PHP file that contains an html code, it just displays it rather than giving its source code in text mode, image below:我制作的上述程序也可以读取文件代码,但是当我单击任何包含 html 代码的 PHP 文件时,它只是显示它而不是在文本模式下提供其源代码,如下图所示:

它的渲染和显示 html

and not only this, if I put some php code at the very end of the image file using a notepad it wouldn't display it.不仅如此,如果我使用记事本在图像文件的最后放置一些 php 代码,它就不会显示它。 check this:检查这个:

图像文件中的php代码

php代码的输出

I did a lot of research on why my code isn't working while the tinyFilemanager is perfect with any of the above mention cases, and I found that the whenever I execute the page file via browser it by default uses this我做了很多研究,为什么我的代码无法正常工作,而 tinyFilemanager 对上述任何情况都很完美,我发现每当我通过浏览器执行页面文件时,它默认使用这个

header("Content-Type: text/html");

so If I wanted to do what I wanted, then I would have to use this:所以如果我想做我想做的事,那么我将不得不使用这个:

header("Content-Type: text/x-php");

which covers both of the above cases, but leads to the 2nd problem.它涵盖了上述两种情况,但导致了第二个问题。

Problem #2:问题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();
}

?>

The above codes works perfectly fine, but there is this one problem.上面的代码工作得很好,但是有一个问题。 since its content-type is not text/html anymore, it wouldn't display the html content on the web page.由于其内容类型不再是 text/html,因此它不会在 web 页面上显示 html 内容。 which is good but bad at the same time because then I wouldn't get the list of directory in the anchor tag form, because I thought ob_start and ob_end_flush().这既好又坏,因为我不会以锚标记形式获得目录列表,因为我认为 ob_start 和 ob_end_flush()。 if I use these two, it would just solve the problem by creating a buffer for each of the function separately and executes it.如果我使用这两个,它将通过为每个 function 分别创建一个缓冲区并执行它来解决问题。 so when it executes it the above function would be render with the content-type text/html and would show the directory listing with anchor tag, and the 2nd would just be in text/x-php which would solve the above two cases, but I was soooooo wrong.因此,当它执行时,上面的 function 将以内容类型 text/html 呈现,并显示带有锚标记的目录列表,第二个将只是在 text/x-php 中,这将解决上述两种情况,但是我错了。

With the grace and help of God, and suggestion from kikoSoftware in the Comments, the Problem is solved, there's a function name show_source();在上帝的恩典和帮助下,以及评论中 kikoSoftware 的建议,问题解决了,有一个 function 名称 show_source(); ,which takes two arguement, the 2nd argument however is optional, hence we don't need to do filing or send a content-type response with the header() function, we can just use that function, source codes are below. ,这需要两个参数,但是第二个参数是可选的,因此我们不需要使用 header() function 进行归档或发送内容类型响应,我们可以使用 function,源代码如下。

<?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.
list_all_files($dir);




if(isset($_GET['read'])){
//changing the header to text/php-x so that the php code in any jpg file can be viewed clearly

    $file1 = $_GET['read'];
    show_source($file1);
}

?>

图片

appreciate ya guys for helping out ♥感谢你们帮忙♥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM