php - 访问根目录之外的文件

标签 php linux apache filesystems debian

我的客户要求我做一个用户可以在机器上输入路径的网站,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>

所以我的问题:

  • 此脚本或此类脚本是否有效?是项目 可行吗?
  • 如果我为 / 创建一个 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 - 访问根目录之外的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37423049/

相关文章:

php - 如何在 Magento 小部件配置中允许选择多个产品?

php - PHP搜寻页面

java - 我已经设置了 apache tomcat 8.5

c++ - 如何将 C++ 用于 apache 服务器

regex - 重新运行或继续规则

php - 不重复重复获取MYSQL结果

php - 使用 PHP 填写 PDF 表单

linux - linux中一个特定字段的长度比较

linux - 如何更改 Linux 显示驱动程序中支持的刷新率?

linux - 设置捕获组的长度