[原创]php+ajax实现模拟Win文件管理系统
//本教程由本站原创,转载请注明来处
作者:www.111cn.net
邮箱:drise@163.com
QQ:271728967//
上面我们讲到了,Deletefile()函数,下面我们接着讲Createfolder()函数
function Createfolder($path,$nname){
if(is_dir($path) && is_writable($path)){//是否为目录且可写
if(preg_match("/^\w{1,255}$/i",$nname)){//判断文件的合法性
echo mkdir($path."/".$nname,0777)?'Create Folder success':'Create Folder Fail';//0777是设置文件可读写
}else{
echo "Folder Error";
}
}else{
echo "Can't Create Error file not is_writable or not dir";
}}
这个函数的功能是实现文件夹的建,
Past($path,$nname,$cpath)函数
function Past($currentpath,$currentfilename,$filepote){ //1:文件要被粘贴到的位置2:当前文件{夹}名3:文件{夹}所在的物理地址
$str = substr($currentfilename,-1,1);
if(substr($currentfilename,-1,1)=="|"){
$currentfilename = str_replace("|","",$currentfilename);
$filepote = str_replace("|","",$filepote);
}
if(is_dir($currentpath) && is_writable($currentpath) && is_dir($filepote) && is_writable($filepote)){
//@mkdir($currentpath."/".$currentfilename);
$t=full_copy($filepote,$currentpath."/".$currentfilename)?'t':'f';//full_copy函数下面接,是进行递归读取文件夹
}else if(is_file($filepote) && file_exists($filepote)){
if(file_exists($currentpath.$currentfilename)){ echo ('file exists! plase rename it!');exit;}
echo copy($filepote,$currentpath.$currentfilename)?'success':'errror';
} if( $str =="|" && $t='t' ){
deldir($filepote);
}
}
function full_copy( $source, $target )//这个函数来自php官方站,功能是进行文件夹递归拷贝文件
{
if ( is_dir( $source ) )
{
@mkdir( $target );
$d = dir( $source );
while ( FALSE !== ( $entry = $d->read() ) )
{
if ( $entry == '.' || $entry == '..' )
{
continue;
}
$Entry = $source . '/' . $entry;
if ( is_dir( $Entry ) )
{
full_copy( $Entry, $target . '/' . $entry );
continue;
}
copy( $Entry, $target . '/' . $entry );
}
$d->close();
}else {
copy( $source, $target );
}
}
上一篇
[原创]php+ajax实现模拟Win文件管理系统
//
本教程由本站原创,转载请注明来处
作者:www.111cn.net
邮箱:drise@163.com
QQ:271728967
//
就是deldir()函数了这个函数的功能是删除文件
function deldir($dir){
if(is_dir($dir)){
$rdir = $dir;
if($dirlist = scandir($rdir)){ //进行扫描目录
array_shift($dirlist);去除".."
array_shift($dirlist);去除"."
foreach($dirlist as $d){
$rd = $rdir.'/'.$d;
if(isset($d) && is_file($rd)){
unlink($rd);//删除文件
}else{
deldir($rd);//递归
}
}
rmdir($rdir);//删除空目录
}else{
return false;
}
}
return true;
}
这个函数也用了递归算法,进行目录与文件的删除,
下面就是filename()函数了,这个函数的功能是对文件进行重命名
function Filename($path,$nname){
// 取得旧文件名
if($path == "" || substr($path,-1) =="/" || strlen($path)>255){
exit('非法操作!');
}else{
$oname = substr($path,strrpos($path,"/")+1);
$opath = substr($path,0,strlen($path) - strlen(substr($path,strrpos($path,"/")+1)));//获取文件的当前路径
}
//重命名
if(preg_match("/^\w{1,255}\.\w{1,8}$/i",$nname,$temp_name) && preg_match("/^\w{1,255}\.\w{1,8}$/i",$oname,$otemp_name)){ //匹配文件名
Rename_file_folder($path,$opath,$nname);//些函数下面讲到
}else if( preg_match("/^\w{1,255}$/i",$nname,$temp_name) && preg_match("/^\w{1,255}$/i",$oname,$otemp_name)){ //匹配文件夹名
Rename_file_folder($path,$opath,$nname);
}else{
echo("File info Error");
}}
function Rename_file_folder($path,$opath,$newname){
if(is_dir($path)){
if(is_writable($path)){
if(is_dir($opath.$newname)){ exit("Sorry Dir is exists");}
echo rename($path,$opath.$newname)?'Rename Success':'Rename Fail Error';
}else{
echo('Can\'t Rename dir not is_writable');
}
}else{
if(file_exists($path) && is_writable($path)){
if(file_exists($opath.$newname)){ exit("file exists"); }
echo rename($path,$opath.$newname)?'Rename success ':'Rename fail';
}else{
echo "file can't is_writable";
}
}
}
上一篇
[原创]php+ajax实现模拟Win文件管理系统
//本教程由本站原创,转载请注明来处
作者:www.111cn.net
邮箱:drise@163.com
QQ:271728967//
上一篇我们己讲了dir.php文件的作用了,下面我就讲rename.php文件每一个函数的功能.
Refere();
header("Content-type:text/html;charset=utf-8");//设置文档编码
$path = isset($_GET['path'])?$_GET['path']:'';
$nname = isset($_GET['nname'])?$_GET['nname']:'';
$cpath = isset($_GET['copypath'])?$_GET['copypath']:'';
$action = isset($_GET['action'])?$_GET['action']:'';
switch($action){
case 'del':
Deletefile($path);
break;
case 'mkdir':
Createfolder($path,$nname);
break;
case 'past':
Past($path,$nname,$cpath);
break;
default:
Filename($path,$nname);
}
这里就是rename.php文件所有函数,下面我们来进行分析.
Refere()函数
很多人看到名称大概就知道什么用意了.下面看它的代码.
function Refere(){
$referer=isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';
if($referer =='' || is_null($referer) || empty($referer)){
exit("Error Server Http 500");
}
}
就这一小段,这里是为了,防止用户直接输入rename.php文件所做的最基本的安全保护方法,函数的就不说了.
Deletefile()函数
function Deletefile($path){
if(is_dir($path) && is_writable($path)){
echo @rmdir($path)?'t':'fo';
}else if(file_exists($path) && is_writable($path)){
echo unlink($path)?'t':'ff';
}else{
exit("没有权限,Error http 404 ");
}
}[上面输出的t,ff这是为后面返回是用ajax传值判断,显示结果给用户看的.]
上一篇
[原创]php+ajax实现模拟Win文件管理系统
//本教程由本站原创,转载请注明来处
作者:www.111cn.net
邮箱:drise@163.com
QQ:271728967//
接上面我们来看dir.php文件代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<script language="javascript"" width=100% src="js/js.js"></script>
<style type="text/css">
<!--
.folder{
height:97px;
width:99px;
border:1px solid #e4e4e4;
text-align:center;
float:left;
margin:20px;
padding-top:15px;
color:#E4E4E4
}
#t{
height:97px;
width:99px;
border:1px solid #e4e4e4;
text-align:center;
float:left;
margin:20px;
padding-top:15px;
color:#E4E4E4;
display:none;
}
p{
text-align:center;
color:#000000;
font-size:12px;
width:90px;
white-space:nowrap;
text-overflow:ellipsis;
-o-text-overflow:ellipsis;
overflow: hidden;
margin-top:15px;
}
input{
width:80px;
border:1px solid #000;
height:18px;
}
#info{
border:1px solid #FF9933;
background-color:#FFFFE1;
display:none;
font-size:12px;
padding-top:7px;
text-align:left;
padding-left:10px;
width:300px;
}
li{
list-style-type:none;
list-style:none;
padding-left:0px;
font-size:12px;
cursor:default;
padding-top:2px;
}
#Layer1{
border:1px solid #28B9FF;
background-color:#ECF9FF;
padding:10px;
padding-top:20px;
line-height:30px;
height:20px;
display:none;
}
.up{width:300px; height:24px;border:1px;}
.bt{width:70px; height:24px;}
#tool{ height:47px; width:98%; margin:0px auto; background-image:url(image/tool_bg.jpg); margin-top:0px;padding-top:0px;padding-left:10px;}
.ta{display:inline-block;width:72px;height:30px; line-height:30px; text-align:center;margin-top:2px; color:#4D4D4D; text-decoration:none}
a.ta:hover{background:url("image/tool_if.gif") no-repeat ; height:30px;}
.aa{ background:url("image/tool_if.gif") no-repeat ; height:40px;}
.bb{ background:#fff;}
body {
margin-left: 0px;
margin-top: 10px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
</head>
<body >
<?php
$dir_path=isset($_GET['url'])?$_GET['url']:'..';
//$totallen = strlen($dir_path);
//$sublen = strlen(substr($dir_path,strrpos($dir_path,"/")));
$lastdir = substr($dir_path,0,strlen($dir_path)-strlen(substr($dir_path,strrpos($dir_path,"/"))));
Tool($dir_path,$lastdir);
CheckDir($dir_path);
//echo "<br>";
//echo substr($dir_path,0,(strlen($dir_path)-strrpos($dir_path,"/")+1));
function CheckDir($dir_path){// 这个函数是检查并显示当前目录下的文件及文件
if(is_dir($dir_path)){
if($dir_file=opendir($dir_path)){
while(($dir_list=readdir($dir_file))!==false)
{
if($dir_list!="." && $dir_list!=".."){//如果加这里判断等一在显示文件列表时.. .这样.
echo("<div class='folder' oncontextmenu=\"return myMenuShow(this,'".$dir_path."/".$dir_list."','".$dir_path."/"."')\" id='".$dir_path."/".$dir_list."' ><a href='?url=".$dir_path."/".$dir_list."'><img" width=100% src='".Getfiletype($dir_list)."' border=0 onload=\"resizepic(this);\" /></a><p id='".$dir_list."' onClick=\"edit(this,'".$dir_path."/".$dir_list."');\" >".$dir_list."</p></div>\n");
}
}
}else{
echo($dir_path."<br/>");
}
}else{
echo($dir_path."<br/>");
}
Folderexp($dir_path);
}
function Getfiletype($dir){//根据扩展名显示相应的图片
global $_GET;
if(@$_GET['url'] !='' and is_dir(@$_GET['url'])){
$dir = $_GET['url'].'\\'.$dir;
}
//print_r( $dir);
if($dir == '' || is_null($dir) || empty($dir)){return false;}
if(is_dir($dir)){
return 'fold.jpg';
}else{
return (Getextends($dir));
}
}
function Getextends($dir){
if($dir =="" || is_null($dir)){return "unkown.jpg";}
$Extends_name = substr($dir,strrpos($dir,".")+1);
$Extends_img = array('jpg','jpeg','gif','png','bmp','psd');
if(in_array($Extends_name,$Extends_img)){return $dir;}
switch($Extends_name){
case "php":
return 'image/asp_php_jsp.jpg';
break;
case "asp":
return 'image/asp_php_jsp.jpg';
break;
case "jsp":
return 'image/asp_php_jsp.jpg';
break;
case "aspx":
return 'image/asp_php_jsp.jpg';
break;
case "xml":
return 'image/asp_php_jsp.jpg';
break;
case "htm":
return 'image/html_js.jpg';
break;
case "html":
return 'image/html_js.jpg';
break;
case "shtml":
return 'image/html_js.jpg';
break;
case "js":
return 'image/html_js.jpg';
break;
case "txt":
return 'image/txt.jpg';
break;
case "doc":
return 'image/word.jpg';
break;
default:
return 'image/fold.jpg';
}
}
function Folderexp($dir_path){//新建文件夹
echo("<div id='t0'></div>");
echo("<div id=\"t\">");
echo("<img" width=100% src=\"image/fold.jpg\" width=\"52\" height=\"50\">");
echo("<p id='t1'>");
echo("<input type=\"text\" value=\"New folder\" onblur=\"Losefocus(this,'".$dir_path."');\"; >");
echo("</p>");
echo("</div>");
}
function Tool($dir_path,$lastdir){//这里显示菜单条
echo("<div id=\"tool\">");
echo("<a href=\"dir.php\" class=\"ta\"><img" width=100% src=\"image/tool_root.gif\" border=\"0\"></a>");
echo("<a href=\"#\" class=\"ta\" onclick=\"history.go(-1);\" ><img" width=100% src=\"image/tool_back.gif\" border=\"0\"></a>");
echo("<a href=\"#\" class=\"ta\" onclick=\"history.go(1);\" ><img" width=100% src=\"image/tool_go.gif\" border=\"0\"></a>");
echo("<a href=\"?url=$lastdir\" class=\"ta\" title='返回上级目录'><img" width=100% src=\"image/tool_s.gif\" border=\"0\"></a>");
echo("<a href=\"#\" class=\"ta\" onclick=\"CreateText();\" title=\"新建文本文件\"><img" width=100% src=\"image/tool_txt.gif\" border=\"0\"></a>");
echo("<a href=\"#\" class=\"ta\" onClick=\"Createfolder();\" title=\"新建文件夹\" ><img" width=100% src=\"image/tool_newfolder.gif\" border=\"0\"></a>");
echo("<a href=\"#\" class=\"ta\" onclick=\"return Past('filepath','".$dir_path."')\" ><img" width=100% src=\"image/tool_past.gif\" border=\"0\"></a>");
echo("<a href=\"#\" class=\"ta\" onClick=\"Upload('".$dir_path."');\" title=\"文件上传\" ><img" width=100% src=\"image/tool_upload.gif\" border=\"0\"></a>");
echo("</div>");
}
?>
<div id="Layer1" style="position:absolute; width:480px; height:20px; z-index:1; left: 293px; top: 143px;">
</div>
</body>
</html>
上一篇
[原创]php+ajax实现模拟Win文件管理系统
//本教程由本站原创,转载请注明来处
作者:www.111cn.net
邮箱:drise@163.com
QQ:271728967//
今天来写一个用php 与ajax 实现模拟Win文件管理系统 一下我就一一讲述了.
首先我们来看年效果图
下面我们就来看看完成这些功能的文件包括,
-
dir.php这文件是列出文件的.
- rename.php这个文件是进行对文件的删除,重命名,拷贝,剪切,新建文件夹等功能的.
- document.js文件这个里右击菜单效果所以单独放在一个文件里面.
- js.js 文件这个文件功能比较多,等一下一下讲解.