[英]Accessing files outside the root directing
我的客戶要求我建立一個網站,用戶可以在該網站上輸入機器上的路徑,PHP應該掃描該路徑並在目錄和子目錄中加載所有媒體文件。 用戶可以在根目錄之外輸入任何路徑,桌面或外部驅動器。 這就是客戶想要的,並且他正在Linux上運行。
我告訴他php無法訪問root以外的文件,他說可以,他說我應該使用一些代理,並且他向我發送了此腳本
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<?PHP
session_start();
function getFileList($dir)
{
// array to hold return value
$retval = array();
// add trailing slash if missing
if(substr($dir, -1) != "/") $dir .= "/";
// open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) {
// skip hidden files
if($entry[0] == ".") continue;
if(is_dir("$dir$entry")) {
$retval[] = array(
"name" => "$entry/"
);
} elseif(is_readable("$dir$entry")) {
$retval[] = array(
"name" => "$entry"
);
}
}
$d->close();
return $retval;
}
$dirlist = getFileList("F:\uni\M2\Thesis\hmayed\ali\songs wav");
// output file list in HTML TABLE format
echo "<table border=\"1\">\n";
echo "<thead>\n";
echo "<tr><th>Name</th></tr>\n";
echo "</thead>\n";
echo "<tbody>\n";
foreach($dirlist as $file) {
echo "<form action=\"MusicP.php\" Method = \"POST\" \">\n";
echo "<input value =\" F:\\uni\\M2\\Thesis\\hmayed\\ali\\songs wav\\{$file['name']}\" type = \"submit\" name= \"submit\" id=\"{$file['name']}\">\n";
echo "</tr>\n";
}
echo "</form>\n";
echo "</table>\n\n";
?>
<audio src = "File:///F:\uni\M2\Thesis\songs\1.mp3" type= "audio/mp3" controls>
Your browser does not support the audio element.
</audio>
<body>
</body>
</html>
所以我的問題是:
/
它會讀取所有的文件系統? 我從未嘗試過。 您可以使用Php腳本代理shell命令以獲取文件列表:
<?php
print nl2br(shell_exec('find /tmp'));
將上面示例中的/ tmp替換為用戶貢獻的值。
此外,要播放媒體文件,您可以執行以下操作(請注意安全性):
<?php
$file = isset($_GET['file']) ? $_GET['file'] : null;
if($file) serve_file($file);
function serve_file($file) {
header('Content-Type: audio/mpeg');
readfile($file);
exit;
}
$dir = '/tmp';
$music = shell_exec("find $dir -name '*.mp3'");
$music = explode("\n", $music);
$music = array_filter($music);
// html here...
foreach($music as $file) {?>
<a href="?file=<?php echo urlencode($file) ?>"><?php echo $file; ?></a><br />
<?
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.