访问根目录之外的文件
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.mp3" type= "audio/mp3" controls>
Your browser does not support the audio element.
</audio>
<body>
</body>
</html>
所以我的问题:
- 此脚本或此类脚本是否有效?是项目
可行吗?
- 如果我为
/
创建一个 apache 虚拟服务器会发生什么情况,它会读取所有文件系统吗?我从来没有尝试过。
您可以使用 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 />
<?
}
我的客户让我做一个网站,用户可以在机器上输入一个路径,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.mp3" type= "audio/mp3" controls>
Your browser does not support the audio element.
</audio>
<body>
</body>
</html>
所以我的问题:
- 此脚本或此类脚本是否有效?是项目 可行吗?
- 如果我为
/
创建一个 apache 虚拟服务器会发生什么情况,它会读取所有文件系统吗?我从来没有尝试过。
您可以使用 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 />
<?
}