php实现文件夹访问,最基础,php在线文件管理系统


php实现的文件夹浏览:
这里写图片描述

<?php
define('ROOTPATH','/tftp/data/');
$spath = isset($_REQUEST)&&isset($_REQUEST['path'])?$_REQUEST['path'].'/':'';
$path = ROOTPATH.$spath;
echo '<hr/>'.xls($path);


// 扫描文件夹子项目
function ls($path){
    if(is_dir($path)){
        return scandir($path);
    }
    return [];
}

// 文件夹信息可视化
function xls($path){
    $dir = ls($path);
    $str = '<table class="fileList">';
    $str .= "<thead><caption><input type=\"text\" value=\"$path\"></caption>";
    $str .= '<th>选择<input type="checkbox"></th>';
    $str .= '<th>名称</th><th>类型</th><th>大小kb</th></thead><tbody>';
    foreach($dir as $index=>$in){
        if(is_dir($path.$in)){
            $str .= '<tr><td><input type="checkbox" disabled></td><td>'.$in.'/</td><td>dir</td><td>--</td><tr>';
        }elseif(is_file($path.$in)){
            $handle = fopen($path.$in,"r");
            $str .= '<tr><td><input type="checkbox"></td><td>'.$in.'</td><td>'.
            getFileType($path.$in).'</td><td>'.round(filesize($path.$in)).'kb</td><tr>';
        }
    }
    $str .= '</tbody></table>';
    return $str;
}

// 根据扩展名判断文件类型
function getFileType($filePath) {
   $exten = explode('.', $filePath);
   return strtolower(end($exten));
}

造轮子造一半,发现有很好的轮子,所以不造了。

一款不错的PHP在线文件管理系统,PHP WEBFTP,挺好用,推荐使用

名字:kodexplorer

官方地址:http://www.kalcaddle.com/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP语言可以轻松地实现文件管理系统。下面是一个简单的示例: 1. 创建一个 index.php 文件并添加以下代码: ``` <?php // 获取当前目录 $dir = isset($_GET['dir']) ? $_GET['dir'] : '/'; // 判断目录是否存在 if (!file_exists($dir)) { echo '目录不存在!'; exit; } // 列出当前目录下的所有文件和子目录 $files = scandir($dir); // 排除 . 和 .. 目录 $files = array_diff($files, array('..', '.')); ?> <!doctype html> <html> <head> <title>文件管理系统</title> </head> <body> <h1><?php echo $dir; ?></h1> <table> <thead> <tr> <th>文件名</th> <th>类型</th> <th>大小</th> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($files as $file): ?> <?php $path = $dir . '/' . $file; $type = is_dir($path) ? '文件夹' : '文件'; $size = is_dir($path) ? '-' : filesize($path); ?> <tr> <td><?php echo $file; ?></td> <td><?php echo $type; ?></td> <td><?php echo $size; ?></td> <td> <?php if (is_dir($path)): ?> <a href="?dir=<?php echo urlencode($path); ?>">查看</a> <?php else: ?> <a href="<?php echo $path; ?>" target="_blank">下载</a> <?php endif; ?> </td> </tr> <?php endforeach; ?> </tbody> </table> </body> </html> ``` 2. 在浏览器中访问 index.php 文件并查看效果。 这段代码使用了 PHP 的 scandir 函数来列出目录中的文件和子目录,并使用 is_dir 函数来判断一个路径是否为文件夹。在页面中,我们使用了一个简单的表格来显示文件列表,并提供了下载和查看子目录的链接。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值