自己写的web文件管理器

Original 2019-05-17 13:24:14 255
abstract://func_lib.php <?php //创建文件 function create_file($fileName) { if(file_exists($fileName)){ return '文件已存在'; } if(!is_dir(dirname($fileName))){ mkdir(dirname($fileName),0777,t
//func_lib.php
<?php
//创建文件
function create_file($fileName)
{
	if(file_exists($fileName)){
		return '文件已存在';
	}
	if(!is_dir(dirname($fileName))){
		mkdir(dirname($fileName),0777,true);
	}

	if(touch($fileName)){
		return '文件创建成功!';
	}
	return '文件创建失败!';
}
// echo create_file('D:\myphp_www\PHPTutorial\WWW\test.php');

//删除文件
function delete_file($fileName)
{
	if(!file_exists($fileName) || !is_writable($fileName)){
		return '文件不存在或没有操作权限';
	}
	if(unlink($fileName)){
		return '文件删除成功!';
	}
	return '文件删除失败!';
}

// echo delete_file('D:\myphp_www\PHPTutorial\WWW\test.php');

//文件复制
function copy_file($fileName,$dest)
{
	if(!file_exists($fileName) || !is_writable($fileName)){
		return '文件不存在或没有操作权限';
	}
	if(!is_dir($dest)){
		mkdir($dest,0777,true);
	}
	$destName = rtrim($dest,'\\/').'/'.basename($fileName);
	if(file_exists($destName)){
		return '目标文件已存在!';
	}
	if(copy($fileName,$destName)){
		return '文件复制成功!';
	}else{
		return '文件复制失败!';
	}
}
// echo copy_file('./a - copy.php','d:/');

//文件重命名
function rename_file($oldName,$newName)
{
	if(!file_exists($oldName) || !is_writable($oldName)){
		return '文件不存在或没有操作权限';
	}
	$newName = dirname($oldName).'/'.$newName;
	if(file_exists($newName)){
		return '目标文件已存在!';
	}
	if(rename($oldName,$newName)){
		return '文件重命名成功!';
	}else{
		return '文件重命名失败!';
	}
}

// echo rename_file('./aa.php','a.php');
function cut_file($fileName,$destDir)
{
	if(!is_file($fileName) || !is_writable($fileName)){
		return '该文件不存在或不可写';
	}
	if(!is_dir($destDir)){
		mkdir($destDir,0777,true);
	}
	$destName = rtrim($destDir,'\\/').'/'.basename($fileName);
	
	if(is_file($destName)){
		return '目标目录已存在同名文件';
	}
	if(rename($fileName,$destName)){
		return '剪切文件成功!';
	}
	return '剪切文件失败!';
}

// echo cut_file('d:/a.php','D:\myphp_www\PHPTutorial\WWW\test');


function get_file_info($fileName)
{
	date_default_timezone_set('Asia/Shanghai');
	if(!is_file($fileName) || !is_writable($fileName)){
		return '该文件或目录不存在或不可写'; 
	}
	return [
		'type'  => filetype($fileName),
		'ctime' => date('Y-m-d H:i:s',filectime($fileName)),
		'mtime' => date('Y-m-d H:i:s',filemtime($fileName)),
		'atime' => date('Y-m-d H:i:s',fileatime($fileName)),
		'size'  => trans_byte(filesize($fileName))
	];
}

// print_r(get_file_info('a.php'));

function trans_byte($byte,$precision = 2)
{
	$kb = 1024;
	$mb = 1024 * $kb;
	$gb = 1024 * $mb;
	$tb = 1024 * $gb;
	if($byte < $kb){
		return $byte.'B';
	}elseif($byte < $mb){
		return round($byte/$kb,$precision).'KB';
	}elseif($byte < $gb){
		return round($byte/$mb,$precision).'MB';
	}elseif($byte < $tb){
		return round($byte/$gb,$precision).'GB';
	}else{
		return round($byte/$tb,$precision).'TB';
	}
}

function read_file($fileName)
{
	if(!file_exists($fileName)){
		return '该文件不存在!';
	}
	return file_get_contents($fileName);
}

function read_file_array($fileName,$skip_empty_lines = false)
{
	if(is_file($fileName) && is_readable($fileName)){
		if($skip_empty_lines){
			return file($fileName,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
		}else{
			return file($fileName);
		}
	}
	return '该文件不存在或不可读';
}

// echo print_r(read_file_array('a.php',true),true);

function write_file($fileName,$data,$clear = false)
{
	$dir = dirname($fileName);
	if(!is_dir($dir)){
		mkdir($dir,0777,true);
	}
	if(is_array($data) || is_object($data)){
		$data = serialize($data);
	}
	if($clear == false){
		if(is_file($fileName) && is_readable($fileName)){
			if(filesize($fileName) > 0){
				$srcData = file_get_contents($fileName);
				$data = $srcData.$data;
			}
		}
	}
	if(file_put_contents($fileName, $data)){
		return '文件写入成功';
	}
	return '文件写入失败';
}
// echo write_file('./file/test.txt',['name'=>'郭靖','age'=>55]);
function load_file($fileName)
{
	header('Accept-Length:'.filesize($fileName));
	header('Content-Disposition:attachment;filename='.basename($fileName));
	readfile($fileName);
	exit;
}

// echo load_file('test.txt');
function upload_file($fileinfo, $destDir='./upload', $allowExt=['jpg','gif','png','txt','php'], $size=1000000)
{
	if($fileinfo['size']>0){
		if(!is_uploaded_file($fileinfo['tmp_name'])){
			return '不是通过表单上传文件';
		}
		$ext = strtolower(pathinfo($fileinfo['name'],PATHINFO_EXTENSION));
		if(!in_array($ext, $allowExt)){
			return '上传文件后缀不允许';
		}
		if($fileinfo['size'] > $size){
			return '上传文件过大';
		}
		if(!is_dir($destDir)){
			mkdir($destDir, 0777, true);
		}
		$destName = rtrim($destDir,'\\/').'/'.md5(uniqid('',true)).'.'.$ext;
		if(!move_uploaded_file($fileinfo['tmp_name'], $destName)){
			return '上传文件失败!';
		}
		return '上传文件成功!';
	}else{
		switch ($fileinfo['error']) {
			case 1:
				# code...
				return '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。 ';
				break;
			case 2:
				return '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。 ';
				break;
			case 3:
				return '文件只有部分被上传。 ';
				break;
			case 4:
			 	return '没有文件被上传。 ';
			 	break;
			case 6:
				return '找不到临时文件夹。';
				break;
			default:
				return '文件写入失败。';
				break;
		}
	}
}

//----------------------目录操作--------------------------

function create_folder($dir)
{
	if(is_dir($dir)){
		return '该目录已存在!';
	}
	if(mkdir($dir, 0777, true)){
		return '目录创建成功!';
	}
	return '目录创建失败!';
}

// echo create_folder('./a/b/c');
function delete_folder($dir)
{
	if(!is_dir($dir)){
		return '该目录不存在';
	}
	$dh = opendir($dir);
	while ($row = readdir($dh)) {
		# code...
		if($row != '.' && $row != '..'){
			$dir = rtrim($dir,'\\/');
			if(is_file($dir.'/'.$row)){
				unlink($dir.'/'.$row);
			}
			if(is_dir($dir.'/'.$row)){
				$func = __FUNCTION__;
				$func($dir.'/'.$row);
			}
		}
	}
	rmdir($dir);
	closedir($dh);
	return '目录删除成功!';
}

// echo delete_folder('./a/');

function copy_dir($dir,$destDir)
{
	$destDir = rtrim($destDir,'\\/').'/'.basename($dir);
	if(is_dir($destDir)){
		return '目标目录已存在';
	}else{
		mkdir($destDir, 0777, true);
	}
	$dh = opendir($dir);
	$dir = rtrim($dir,'\\/');
	$destDir = rtrim($destDir,'\\/');
	while ($row = readdir($dh)) {
		if($row != '.' && $row != '..'){
			if(is_file($dir.'/'.$row)){
				copy($dir.'/'.$row,$destDir.'/'.$row);
			}
			if(is_dir($dir.'/'.$row)){
				$func = __FUNCTION__;
				$func($dir.'/'.$row,$destDir.'/'.$row);
			}
		}		
	}
	closedir($dh);
	return '目录复制成功';
}

// echo copy_dir('D:\2345Downloads\\','D:\myphp_www\PHPTutorial\WWW\2345Downloads\\');
function rename_dir($oldDir, $newDir)
{
	if(is_dir($newDir)){
		return '目标目录已存在!';
	}
	$newDir = dirname($oldDir).'/'.basename($newDir);
	if(rename($oldDir, $newDir)){
		return '目录重命名成功!';
	}

	return '目录重命名失败!';
}
// echo rename_dir('./file','./file1');
function cut_dir($srcDir, $destDir)
{
	if(is_dir($destDir)){
		$destDir = rtrim($destDir,'\\/').'/'.basename($srcDir);
		if(file_exists($destDir)){
			if(rename($srcDir, $destDir)){
				return '目录剪切成功!';
			}else{
				return '目录剪切失败!';
			}
		}else{
			return '目标目录已存在!';
		}
		
	}else{
		return '目标不是一个目录';
	}
}

// echo cut_dir('./a','../');

function read_dir($dir)
{
	$arr = [];
	$dh = opendir($dir);
	while($row = readdir($dh)){
		if($row != '.' && $row != '..'){
			$dir = rtrim($dir,'\\/');
			if(is_file($dir.'/'.$row)){
				$arr['file'][] = $row;
			}
			if(is_dir($dir.'/'.$row)){
				$arr['dir'][] = $row;
			}
		}
	}
	closedir($dh);
	return $arr;
}

// echo print_r(read_dir('./'),true);
function dir_size($dir)
{
	static $num = 0;
	$dh = opendir($dir);
	while(($row = readdir($dh)) !== false){
		if($row!='.' && $row!='..'){
			$dir = rtrim($dir,'\\/');
			if(is_file($dir.'/'.$row)){
				$num += filesize($dir.'/'.$row);
			}
			if(is_dir($dir.'/'.$row)){
				$func = __FUNCTION__;
				$func($dir.'/'.$row);
			}
		}
	}
	closedir($dh);
	return trans_byte($num);
}
// echo trans_byte(dir_size('../oa'));
//file.php
<?php

require_once '../func_lib.php';

$path = '../';


@$path = $_REQUEST['path']?:$path;

$data = read_dir($path);

@$act = $_REQUEST['act'];

@$filename = $_REQUEST['filename'];

@$dirname = $_REQUEST['dirname'];


$url = $_SERVER['PHP_SELF'].'?path='.$path;


require './operation.php';

require './muban.php';

//muban.php
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PHP中文网—Web在线文件管理器</title>
    <link rel="stylesheet" href="css/cikonss.css"/>
    <link rel="stylesheet" href="css/style.css"/>
    <link rel="stylesheet" href="css/jquery-ui-1.10.4.custom.css" type="text/css"/>
    <script src="js/jquery-1.10.2.js"></script>
    <script src="js/jquery-ui-1.10.4.custom.js"></script>
    <script src="js/action.js"></script>
</head>
<body>
<h1>PHP中文网—Web在线文件管理器</h1>
<div id="showDetail" style="display:none"><img src="" id="showImg" alt=""/></div>
<div id="top">
    <ul id="navi">
        <li><a href="file.php" title="主目录"><span style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span></span></span></a></li>
        <li><a href="#" onclick="show('createFile')" title="新建文件"><span
                        style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span
                           ></span></span></a></li>
        <li><a href="#" onclick="show('createFolder')" title="新建文件夹"><span
                        style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span
                           ></span></span></a></li>
        <li><a href="#" onclick="show('uploadFile')" title="上传文件"><span
                        style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span
                           ></span></span></a></li>
        <li><a href="#" title="返回上级目录" onclick="goBack('')"><span
                        style="margin-left: 8px; margin-top: 0px; top: 4px;" class="icon icon-small icon-square"><span
                           ></span></span></a></li>
    </ul>
</div>
<form action="file.php" method="post" enctype="multipart/form-data">
    <table width="100%" border="1" cellpadding="5" cellspacing="0" bgcolor="#ABCDEF">
        <tr id="createFolder" style="display:none;">
            <td>请输入文件夹名称</td>
            <td>
                <input type="text" name="dirname"/>
                <input type="hidden" name="path" value="<?php echo $path;?>"/>
                <input type="submit" name="act" value="创建文件夹"/>
            </td>
        </tr>
        <tr id="createFile" style="display:none;">
            <td>请输入文件名称</td>
            <td>
                <input type="text" name="filename"/>
                <input type="hidden" name="path" value="<?php echo $path;?>"/>
                <input type="submit" name="act" value="创建文件"/>
            </td>
        </tr>
        <tr id="uploadFile" style="display:none;">
            <td>请选择要上传的文件</td>
            <td><input type="file" name="myFile"/>
                <input type="submit" name="act" value="上传文件"/>
            </td>
        </tr>
    </table>
</form>
<table width="100%" border="1" cellpadding="5" cellspacing="0" bgcolor="#ABCDEF">
    <tr>
        <th>编号</th>
        <th>名称</th>
        <th>类型</th>
        <th>大小</th>
        <th>可读</th>
        <th>可写</th>
        <th>可执行</th>
        <th>创建时间</th>
        <th>修改时间</th>
        <th>访问时间</th>
        <th>操作</th>
    </tr>
    <?php $i=1;if(!empty($data['file'])):?>
    <?php foreach($data['file'] as $v):?>
    <?php $p = rtrim($path,'\\/').'/'.$v;$info=get_file_info($p);?>
    <tr>
        <td><?php echo $i;?></td>
        <td><?php echo iconv('GBK','UTF-8',$v);?></td>
        <td><img src="images/file_ico.png" title="文件"></td>
        <td><?php echo $info['size'];?></td>
        <td><?php $src=is_readable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>"></td>
        <td><?php $src=is_writable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>"></td>
        <td><?php $src=is_executable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>"></td>
        <td><?php echo $info['ctime'];?></td>
        <td><?php echo $info['mtime'];?></td>
        <td><?php echo $info['atime'];?></td>
        <td>
            <?php
            $ext = pathinfo($v,PATHINFO_EXTENSION);
            $allowExt = ['jpg','jpeg','gif','png'];
            if(in_array($ext, $allowExt)):
            ?>
            <a href="javascript:showDetail('<?php echo $v;?>','<?php echo $p;?>');"><img src="images/show.png" alt="" title="查看"/></a>
            <?php else:?>
            <a href="?act=showContent&path=<?php echo $path;?>&filename=<?php echo $p;?>"><img src="images/show.png" alt="" title="查看"/></a>    
            <?php endif;?>
            <a href="?act=editContent&path=<?php echo $path;?>&filename=<?php echo $p;?>"><img src="images/edit.png" alt="" title="修改"/></a>
            <a href="?act=renameFile&path=<?php echo $path;?>&filename=<?php echo $p;?>"><img src="images/rename.png" alt="" title="重命名"/></a>
            <a href="?act=copyFile&path=<?php echo $path;?>&filename=<?php echo $p;?>"><img src="images/copy.png" alt="" title="复制"/></a>
            <a href="?act=cutFile&path=<?php echo $path;?>&filename=<?php echo $p;?>"><img src="images/cut.png" alt="" title="剪切"/></a>
            <a href="?act=loadFile&path=<?php echo $path;?>&filename=<?php echo $p;?>"><img src="images/download.png" alt="" title="下载"/></a>
            <a href="?act=delFile&path=<?php echo $path;?>&filename=<?php echo $p;?>" onclick="delFile('<?php echo iconv('GBK', 'UTF-8', $p);?>','<?php echo $path;?>')"><img src="images/delete.png" alt="" title="删除"/></a>
        </td>
    </tr>
    <?php $i++;endforeach;?>
    <?php endif;?>

    <?php if(!empty($data['dir'])):?>
    <?php $j=$i;foreach($data['dir'] as $v):?>
    <?php $p = rtrim($path,'\\/').'/'.$v;?>
    <tr>
        <td><?php echo $j;?></td>
        <td><?php echo $v;?></td>
        <td><img src="images/folder_ico.png" title="文件夹"></td>
        <td><?php echo dir_size($p);?></td>
        <td><?php $src=is_readable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>"></td>
        <td><?php $src=is_writable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>"></td>
        <td><?php $src=is_executable($p)?'correct.png':'error.png';?><img src="images/<?php echo $src;?>"></td>
        <td><?php echo date('Y-m-d H:i:s',filectime($p));?></td>
        <td><?php echo date('Y-m-d H:i:s',filemtime($p));?></td>
        <td><?php echo date('Y-m-d H:i:s',fileatime($p));?></td>
        <td>
            <a href="?path=<?php echo $p;?>"><img src="images/show.png" alt="" title="查看"/></a>
            <a href="?act=renameDir&path=<?php echo $path;?>&dirname=<?php echo $p;?>"><img src="images/rename.png" alt="" title="重命名"/></a>
            <a href="?act=copyDir&path=<?php echo $path;?>&dirname=<?php echo $p;?>"><img src="images/copy.png" alt="" title="复制"/></a>
            <a href="?act=cutDir&path=<?php echo $path;?>&dirname=<?php echo $p;?>"><img src="images/cut.png" alt="" title="剪切"/></a>
            <a href="#" onclick="delFolder('<?php echo $p;?>','<?php echo $path;?>')"><img src="images/delete.png" alt="" title="删除"/></a>
        </td>
    </tr>
    <?php $j++;endforeach;?>
    <?php endif;?>
</table>
</body>
</html>

//operation.php
<?php
include './common.php';
if($act == '创建文件夹'){
	$res = create_folder(rtrim($path,'\\/').'/'.$dirname);
	alertMsg($res,$url);
}elseif($act == '创建文件'){
	$res = create_file(rtrim($path,'\\/').'/'.$filename);
	alertMsg($res,$url);
}elseif($act == 'showContent'){//查看文件内容
	$content = read_file($filename);
	if(strlen($content)){
		$newContent = highlight_string($content,true);
		$str = <<<HERE
			<table width="100%" cellpadding="5" cellspacing="0">
				<tr>
					<td>{$newContent}</td>
				</tr>
			</table>
HERE;
		echo $str;

	}else{
		alertMsg('该文件是空的!请编辑!',$url);
	}
}elseif($act == 'editContent'){//显示修改文件内容模板
	$content = read_file($filename);
	$str = <<<HERE
	<form action="?act=doEdit" method='post'>
		请输入内容:<textarea name="content" cols="30" rows="10">{$content}</textarea>
		<input type="hidden" name="path" value="{$path}">
		<input type="hidden" name="filename" value="{$filename}">
		<input type="submit" value='提交'>
	</form>
HERE;
	echo $str;
}elseif($act == 'doEdit'){//修改文件操作
	$content = $_REQUEST['content'];
	$res = write_file($filename,$content,$clear = true);
	alertMsg($res, $url);
}elseif($act == 'renameFile'){//重命名文件模板
	$name = basename($filename);
	$str = <<<HERE
	<form action="?act=doRenameFile" method="post">
		请输入新名称:<input type="text" name="newname" value="{$name}">
		<input type="hidden" name="path" value="{$path}">
		<input type="hidden" name="filename" value="{$filename}">
		<input type="submit" value="提交">
		</form>
HERE;
	echo $str;
}elseif($act == 'doRenameFile'){//重命名文件
	$newname = $_REQUEST['newname'];
	$res = rename_file($filename, $newname);
	alertMsg($res, $url);
}elseif($act == 'renameDir'){//重命名目录模板
	$name = basename($dirname);
	$str = <<<HERE
	<form action="?act=doRenameDir" method="post">
		请输入新名称:<input type="text" name="newname" value="{$name}">
		<input type="hidden" name="dirname" value="{$dirname}">
		<input type="hidden" name="path" value="{$path}">
		<input type="submit" value="提交">
		</form>
HERE;
	echo $str;
}elseif($act == 'doRenameDir'){//重命名目录
	$newname = $_REQUEST['newname'];
	$res = rename_dir($dirname, $newname);
	alertMsg($res, $url);
}elseif($act == 'copyFile'){//复制文件模板
	$str = <<<HERE
	<form action="?act=doCopyFile" method="post">
		复制到:<input type="text" name="destDir">
		<input type="hidden" name="path" value="{$path}">
		<input type="hidden" name="filename" value="{$filename}">
		<input type="submit" value="复制">
		</form>
HERE;
	echo $str;
}elseif($act == 'doCopyFile'){//复制文件
	$destDir = $_REQUEST['destDir'];
	$res = copy_file($filename,rtrim($path,'\\/').'/'.$destDir);
	alertMsg($res, $url);
}elseif($act == 'cutFile'){//剪切文件模板
	$str = <<<HERE
	<form action="?act=doCutFile" method="post">
		剪切到:<input type="text" name="destDir">
		<input type="hidden" name="path" value="{$path}">
		<input type="hidden" name="filename" value="{$filename}">
		<input type="submit" value="剪切">
		</form>
HERE;
	echo $str;
}elseif($act == 'doCutFile'){//剪切文件
	$destDir = $_REQUEST['destDir'];
	$res = cut_file($filename, rtrim($path,'\\/').'/'.$destDir);
	alertMsg($res, $url);
}elseif($act == 'copyDir'){//复制目录模板
	$folder = basename($dirname);
	$str = <<<HERE
	<form action="?act=doCopyDir" method="post">
		把{$folder}文件夹复制到:<input type="text" name="destDir">
		<input type="hidden" name="dirname" value="{$dirname}">
		<input type="hidden" name="path" value="{$path}">
		<input type="submit" value="复制">
		</form>
HERE;
	echo $str;
}elseif($act == 'doCopyDir'){//复制目录
	$destDir = $_REQUEST['destDir'];
	$res = copy_dir($dirname, rtrim($path,'\\/').'/'.$destDir);
	alertMsg($res, $url);
}elseif($act == 'cutDir'){//剪切目录模板
	$folder = basename($dirname);
	$str = <<<HERE
	<form action="?act=doCutDir" method="post">
		把{$folder}文件夹剪切到:<input type="text" name="destDir">
		<input type="hidden" name="dirname" value="{$dirname}">
		<input type="hidden" name="path" value="{$path}">
		<input type="submit" value="剪切">
		</form>
HERE;
	echo $str;
}elseif($act == 'doCutDir'){//剪切目录
	$destDir = $_REQUEST['destDir'];
	$res = cut_dir($dirname, rtrim($path,'\\/').'/'.$destDir);
	alertMsg($res, $url);
}elseif($act == 'loadFile'){//下载文件
	//echo $filename;exit;
	load_file($filename);
}elseif($act == 'delFile'){//删除文件
	$filename = iconv('UTF-8', 'GBK', $filename);
	$res = delete_file($filename);
	alertMsg($res, $url);
}elseif($act == '上传文件'){//上传文件
	$fileInfo = $_FILES['myFile'];
	$res = upload_file($fileInfo,'../upload');
	alertMsg($res, $url);
}elseif($act == 'delFile'){//删除文件
	$res = delete_file(rtrim($path,'\\/').'/'.$filename);
	alertMsg($res, $url);
}elseif($act == 'delFolder'){//删除目录
	$res = delete_folder(rtrim($path,'\\/').'/'.$dirname);
	alertMsg($res, $url);
}
//common.php
<?php

function alertMsg($msg,$url)
{
	echo "<script>alert('{$msg}');location.href='{$url}';</script>";
}

QQ图片20181114103819.png

Correcting teacher:天蓬老师Correction time:2019-05-17 17:44:02
Teacher's summary:php的文档管理功能, 与其它语言相比, 还是很简单的, 但效率还是可以的

Release Notes

Popular Entries