📜  php 目录列表 - PHP (1)

📅  最后修改于: 2023-12-03 15:33:38.939000             🧑  作者: Mango

PHP 目录列表

PHP 目录列表是一个简单的 PHP 程序,可以方便地展示目录下的所有文件和子目录,并提供一些常见的文件操作功能。通常用于搭建简单的文件共享或下载站点。

功能

PHP 目录列表主要具有以下功能:

  • 显示指定目录下的所有文件和子目录
  • 对可执行文件提供在线预览和运行功能
  • 支持文件下载和删除
  • 支持创建新目录和上传文件
代码示例

以下是 PHP 目录列表的基本实现示例:

<?php
$path = isset($_GET['path']) ? $_GET['path'] : '.';
// 检查目录是否存在并且可读
if (!is_dir($path) || !is_readable($path)) {
    die('目录不存在或不可读');
}
// 获取目录下的所有文件和子目录
$files = scandir($path);
// 过滤掉 . 和 .. 目录
$files = array_filter($files, function ($file) {
    return $file != '.' && $file != '..';
});
// 对文件和子目录进行分类
$directories = array_filter($files, function ($file) use ($path) {
    return is_dir($path . '/' . $file);
});
$files = array_filter($files, function ($file) use ($path) {
    return !is_dir($path . '/' . $file);
});
// 显示目录列表
echo '<ul>';
foreach ($directories as $directory) {
    echo '<li><a href="?path=' . $path . '/' . $directory . '">' . $directory . '</a></li>';
}
foreach ($files as $file) {
    if (is_executable($path . '/' . $file)) {
        // 显示可执行文件的预览和运行链接
        echo '<li>' . $file . ' [<a href="?path=' . $path . '&action=preview&file=' . $file . '">预览</a> <a href="?path=' . $path . '&action=run&file=' . $file . '">运行</a>]</li>';
    } else {
        // 显示普通文件的下载和删除链接
        echo '<li>' . $file . ' [<a href="' . $path . '/' . $file . '">下载</a> <a href="?path=' . $path . '&action=delete&file=' . $file . '">删除</a>]</li>';
    }
}
echo '</ul>';
// 显示上传表单和新目录创建表单
echo '<form method="post" enctype="multipart/form-data">
<label for="file">上传文件</label><input type="file" name="file" id="file">
<input type="hidden" name="path" value="' . $path . '">
<button type="submit">上传</button>
</form>';
echo '<form method="post">
<label for="dir">创建目录</label><input type="text" name="dir" id="dir">
<input type="hidden" name="path" value="' . $path . '">
<button type="submit">创建</button>
</form>';
// 处理上传和创建操作
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_FILES['file'])) {
        $upload_file = $_FILES['file']['tmp_name'];
        $target_file = $path . '/' . $_FILES['file']['name'];
        move_uploaded_file($upload_file, $target_file);
        header('Location: ?path=' . $path);
        exit;
    } elseif (isset($_POST['dir'])) {
        $new_dir = $path . '/' . $_POST['dir'];
        mkdir($new_dir);
        header('Location: ?path=' . $path);
        exit;
    }
}
// 处理预览、运行和删除操作
if (isset($_GET['action']) && isset($_GET['file'])) {
    switch ($_GET['action']) {
        case 'preview':
            echo '<pre>' . htmlspecialchars(file_get_contents($path . '/' . $_GET['file'])) . '</pre>';
            break;
        case 'run':
            include $path . '/' . $_GET['file'];
            break;
        case 'delete':
            unlink($path . '/' . $_GET['file']);
            header('Location: ?path=' . $path);
            exit;
    }
}
注意事项
  • 由于 PHP 目录列表可以访问服务器上的任意文件,所以一定要谨慎部署,并对访问进行适当的限制和身份验证。
  • PHP 目录列表不支持文件和子目录的重命名和移动功能,如有需要需要自行扩展。