最近又来折腾了,突发奇想用PHP写一个文件管理器,所谓文件管理器,就只有一个读取文件夹与修改文件而已,删除文件以及文件夹由于懒,还没有写,代码写的也挺乱,变量名称也不是太规范,哈哈,将就着看吧。有很多的缺点,当时写的时候没有想仔细,就一下子开始写了,比如读取文件类返回的是li,不够灵活。另外文件图标方面尝试过用访问路径的方式来判断是否存在,但是发现文件一多的话,就会速度很慢,所以用了直接选用同文件夹下的后缀名.png的这个图标,只用if语句简单的判断了几种特殊的方式,好啦,不多说了共享代码
原文地址:http://renpengpeng.com/741.html
文件
index.php //首页文件
edit.php //编辑文件文件
class.dir.php //封装类文件
文件夹页面
编辑文件页面
class.dir.php
<?php
/*
**文件管理类
*/
error_reporting(0);
class dir{
public $path; //路径
public $opendirr; //打开路径
public $readdirr;//读取路径
public $filee; //文件数组
public $doc_root;//站点根目录
public $zsPath; //用于拼接目录
public $fileStar; //文件属性
public $strip; //字符最后一次出现的位置
public $strlen; //字符总长度
public $sub; //截取的文件后缀名
public $fileEvenImg; //每个不同的文件属性的图标
public $alert = "文件暂时不能读取,新功能开发中!";
public $fileOutChar; //原编码
public $pathChar; //路径字符编码
public $pathGbk; //路径gbk编码
public function __construct(){
$this->doc_root = $_SERVER['DOCUMENT_ROOT'];
}
//析构函数打开文件
public function odir($path){
$this->path = $path;
if(!$path){
die("error 没有定义路径请定义路径!");
}
//开始获取路径编码并强行转为gbk
//然后用gbk的方式来获取文件后强行转为utf-8显示
$this->pathChar = mb_detect_encoding($this->path,array('UTF-8','GBK','LATIN1','BIG5'));
$this->pathChar = strtolower($this->pathChar);
if($this->pathChar != 'gbk'){
$this->pathGbk = iconv($this->pathChar, 'gbk',$this->path );
}
if(!is_dir($this->pathGbk)){
die("error 不是一个正确的路径!");
}
$this->opendirr = opendir($this->pathGbk);
if(!$this->opendirr){
die('error 打开文件夹失败!');
}
while($this->filee = readdir($this->opendirr)){
/*获取编码值不是则转换为utf-8 */
$charset = mb_detect_encoding($this->filee,array('UTF-8','GBK','LATIN1','BIG5'));
$charset = strtolower($charset);
/* 转换为utf-8 */
if($charset != 'utf-8' || $charset != 'utf8'){
//$char = iconv($charset, 'cp960', $this->filee);
$this->filee = iconv($charset, 'utf-8', $this->filee);
}
/*附一个值为原编码 * 方便浏览访问+判断 */
$this->fileOutChar = iconv('utf-8', 'gbk', $this->filee);
$this->pathGbk = iconv('utf-8', 'gbk', $this->path);
/* 开始switch判断把默认的 . 与 ..改成返回上一页与反回首页 */
switch ($this->filee) {
case '.':
echo "<li><a href='javascrip::valid(0)' onclick='window.history.back(-1);return false;'><img src='img/back.png' width='25' height='25'>返回上级</a></li>";
break;
case '..':
echo "<li><a href='index.php?url={$this->doc_root}'><img src='img/index.png' width='25' height='25'>站点首页</a></li>";
break;
default :
/* $this->zsPath 拼接的gbk路径 用于判断是否为文件夹 */
$this->zsPath = $this->pathGbk.'/'.$this->fileOutChar;
if(is_dir($this->zsPath)){
echo "<li><img src='img/files.png' width='30' height='30'><a href='index.php?path={$this->path}/{$this->filee}'>".$this->filee."</a></li>";
}else {
/* 开始获取文件后缀名 */
$this->strip = strripos($this->filee,'.');
$this->strlen = strlen($this->filee);
$this->sub = substr($this->filee,$this->strip+1,$this->strlen - $this->strip);
//截取完毕开始判断 * 如果没有后缀名则使用no.png
if(!$this->strip){
$this->sub = 'no';
}
//如果后缀名数字多余两位 使用nums.png
if(preg_match('/[0-9]{2,}/',$this->sub)){
$this->sub = 'nums';
}
//如果后缀名中带‘ - ’则使用no.png
if(preg_match('-', $this->sub)){
$this->sub = 'no';
}
//如果后缀名大于等于6位则使用no.png
if(strlen($this->sub) >= 6){
$this->sub = 'no';
}
//否则使用img目录对于的后缀名
echo "<li><img src='img/{$this->sub}.png' width='30' height='30'><a href='edit.php?f={$this->path}/{$this->filee}'>".$this->filee."</a></li>";
}
break;
}
}
closedir($this->opendirr);
}
}
/** 读取文件类 **/
class edit {
public $file;
public $filename; //文件名字
public $filetype; //文件类型
public $filesize; //文件大小
public $fileopen; //打开文件
public $fileread; //读取文件
//写入变量
public $filepath;
public $filecontent; //保存文件用到的文件内容
public $fileput; //文件写入
public function edits($files){
$this->file = $files;
if(!file_exists($this->file)){
die("文件不存在!");
}
$this->fileopen = fopen($this->file, 'r');
if(!$this->fileopen){
die("文件读取失败");
}
$this->filesize = filesize($this->file);
$this->fileread = fread($this->fileopen,$this->filesize);
$this->fileread = htmlspecialchars($this->fileread);
//开始获取文件的后缀名
$filestr = strlen($this->file);
$filepoint = strrpos($this->file, '.');
$filesub = substr($this->file, $filepoint+1);
$this->filetype = $filesub;
$this->filename = basename($this->file);
//以数组形式返回:文件类型 文件名称 文件内容 文件路径
return array(
'filetype'=>$this->filetype,
'filename'=>$this->filename,
'filecontent'=>$this->fileread,
'filepath'=>$this->file
);
//关闭文件
fclose($this->file);
}
/* 修改文件函数 */
public function bc($filepath,$filecontent){
$this->filepath = $filepath;
//访问文件
$this->fileopen = file_get_contents($this->filepath);
if(!$this->fileopen){
die('文件打开失败');
}
//获取传进来的文件内容
$this->filecontent = $filecontent;
//反转义html
$this->filecontent = htmlspecialchars_decode($this->filecontent);
//写入文件
$this->fileput = file_put_contents($this->filepath, $this->filecontent);
//如果成功返回true 否则返回false
if($this->fileput){
return true;
}else {
return false;
}
}
}
?>
edit.php
<?php
//读取文件edit.php
require_once 'class.dir.php';
if(!isset($_GET['f'])){
die("no error");
}
$edit = new edit();
$edits = $edit->edits($_GET['f']);
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $edits['filename']; ?>-编辑</title>
<style type="text/css">
* {
padding: 0;
margin: 0;
border: 0;
}
.box {
width: 800px;
margin: 0 auto;
border: 1px solid #eee;
}
.title {
text-align: center;
height: 80px;
line-height: 80px;
}
.bjcontent {
min-height:500px;
}
textarea {
width: 100%;
margin: 0 auto;
min-height: 500px;
border: 1px solid #000;
padding: 5px;
outline: none;
}
.but {
text-align: center;
}
.but button{
margin-top:10px;
width: 60px;
height: 30px;
text-align: center;
line-height: 30px;
border: 0;
background: blue;
color: #fff;
outline: none;
}
.marleft {
margin-left: 10px;
}
</style>
</head>
<body>
<div class="box">
<div class="title"><?php echo $edits['filename']; ?>-编辑</div>
<div class="bjcontent">
<form action='' method="post">
<textarea name='filenr'>
<?php echo $edits['filecontent']; ?>
</textarea>
</div>
<div class='but'><button name='bc'>保存</button><button name='out' class="marleft" onclick="window.history.back(-1);return false;">取消</button></div>
</div>
<?php
if(isset($_POST['bc'])){
//把总路径减去文件名得到上级目录
$tzPath = str_replace($edits['filename'], '', $edits['filepath']);
//得到url
$tzUrl = "index.php?path=".$tzPath;
$filecontent = $_POST['filenr'];
$xieru = $edit->bc($edits['filepath'],$filecontent);
if(!$xieru){
echo "<script>alert('保存失败')</script>";
echo "<script>location.href='{$tzUrl}'</script>";
}else {
echo "<script>alert('保存成功')</script>";
echo "<script>location.href='{$tzUrl}'</script>";
}
}
?>
</body>
</html>
index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="icon" href="http://localhost/dir/favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="http://localhost/dir/favicon.ico" type="image/x-icon">
<style type="text/css">
* {
padding: 0;
margin: 0;
border: 0;
}
.file {
margin: 0 auto;
width: 800px;
height: auto;
padding: 15px;
border: 1px solid #ddd;
}
.file ul {
width: 100%;
list-style: none;
}
.file li {
width: 100%;
height: 50px;
line-height: 50px;
font-size: 16px;
border-bottom: 1px solid #eaeaee;
}
.file li a {
color:#000;
text-decoration: none;
}
.file li img {
margin-bottom: -10px;
margin-right: 5px;
}
.bd input {
border-radius: 10px;
outline: none;
}
.bdinp {
border: 1px solid #000;
height: 40px;
width: 80%;
}
.bdsub {
border: 0;
background:#ccc;
width: 15%;
height: 40px;
}
</style>
</head>
<body>
<div class="file">
<ul>
<?php
require_once 'class.dir.php';
if($_GET['path']){
$path = $_GET['path'];
}else {
$path = $_SERVER['DOCUMENT_ROOT'];
}
$dir = new dir();
$dir->odir($path);
?>
</ul>
</div>
</body>
</html>
压缩包下载地址:dir.zip