我正在嘗試用 php 制作一個檔案管理器,所以當我在瀏覽器中打開它時,它會給出當前目錄的串列,并且可以使用 html 中的錨標記(我到目前為止已經完成)點擊該檔案,以及何時單擊該檔案,它將以文本模式打開它并顯示檔案中的任何源代碼。
我面臨兩個我無法弄清楚的問題
問題#1:
第一個問題是我希望我的檔案管理器能夠讀取任何源代碼,無論是影像還是 pdf ,就像我在
不僅如此,如果我使用記事本將一些 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();
}
?>
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. 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(). if I use these two, it would just solve the problem by creating a buffer for each of the function separately and executes it. 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.
uj5u.com熱心網友回復:
在大神的幫助下,加上kikoSoftware在評論中的建議,問題解決了,有個函式名show_source(); ,這需要兩個引數,但是第二個引數是可選的,因此我們不需要使用 header() 函式進行歸檔或發送內容型別回應,我們可以使用該函式,源代碼如下。
<?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);
}
?>

感謝你們的幫助?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/370686.html
標籤:php file fopen file-manager
