当前位置:编程学习 > php >>

ajax+php文件管理系统

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.zzzyk.com/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="cache-control" content="no-cache, must-revalidate">
<title>ajax+php教程文件管理系统</title>
<style type="text/css教程">
@charset "utf-8";
/* css document */
<!--
body {
 padding-right: 0px;
 padding-left: 0px;
 padding-bottom: 0px;
 margin: 0px auto;
 color: #777;
 line-height: 18px;
 padding-top: 0px;
 font-family: tahoma, arial, helvetica, sans-serif;
 background-color: #fff
}
*+html body {
 padding-right: 0px;
 padding-left: 0px;
 padding-bottom: 0px;
 margin: 0px auto;
 color: #777;
 line-height: 18px;
 padding-top: 0px;
 font-family: "微软雅黑", ahoma, arial, helvetica, sans-serif;
 background-color: #fff
}
a {
 text-decoration:none
}
input {
 padding-right: 0px; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px
}
.main {
 background-color:#bfe6bc;
 margin:100px auto;
 width:800px;
 height:180px;
 border:8px #399c26 solid;
}
.header {
 text-align:center;
 margin:10px auto;
 width:100%;
 display:block
}
.header h1 {
 font-size:20px;
 color:#399c26
}
.header p {
 padding:0px;
 margin-top:-10px;
}
.header p span{
 font-size:12px;
 color:#fff
}
.button_list {
    margin:10px auto;
 width:382px;
 height:110px;
 display:block
}
.button {
 width:100px;
 border:8px #399c26 solid;
 height:20px;
 line-height:20px;
 font-size:14px;
 float:left;
 text-align:center;
 margin-left:10px;
 margin-bottom:10px;
 background-color:#399c26
}
.button a{
 color:#fff;
 font-weight:bold
}
.footer {
 text-align:center;
 font-size:12px;
 display:block;
 margin:10px auto;
}
.black_overlay{
 display: none;
 position: absolute;
 top: 0%;
 left: 0%;
 width: 100%;
 height: 100%;
 background-color: black;
 z-index:1001;
 -moz-opacity: 0.8;
 opacity:.80;
 filter: alpha(opacity=80);
}
.file_list {
 display: none;
 position: absolute;
 top: 25%;
 left: 25%;
 width: 50%;
 height: 50%;
 padding: 16px;
 border: 8px solid #399c26;
 background-color: white;
 z-index:1002;
 overflow: auto;
 background-color:#bfe6bc;
}
.file_list_t {
 margin:0 auto;
 display:inline;
}
.file_list_t li {
 font-size:14px;
 list-style-type:none;
 padding:0px;
 background-color:#bfe6bc;
 border-bottom:1px #ccc solid;
}
*+html .file_list_t li {
 font-size:14px;
 background:url(images/list_ico_1.gif) #fff no-repeat 7px 50%;
 list-style-type:none;
 text-indent:5px;
 padding:0px;
 background-color:#bfe6bc;
 border-bottom:1px #ccc solid;
}
*+html .file_list_t li span {
 float:right;
 font-weight:bold;
 margin-top:-20px;
}
.file_list_t li span {
 float:right;
 font-weight:bold;
 margin-top:0px;
}
.file_list_t li a{
 color:#399c26;
}
.file_list_close {
 display: none;
 position: absolute;
 top: 22%;
 left: 75%;
 width: 30px;
 height: 16px;
 border: 1px solid #399c26;
 z-index:1002;
 background-color:#399c26;
 color:#fff;
 line-height:16px;
 text-align:center
}
.file_list_close a{
 color:#fff;
}

/* file_content_input_form */
.file_content_input_form{
    margin:0 auto;
    width:450px;
    padding:14px;
}
/* input_box */
#input_box{
}
#input_box h1 {
    font-size:14px;
    font-weight:bold;
    margin-bottom:8px;
}
#input_box p{
    font-size:11px;
    color:#399c26;
    margin-bottom:20px;
    border-bottom:solid 1px #b7ddf2;
    padding-bottom:10px;
}
#input_box label{
    display:block;
    font-weight:bold;
    text-align:right;
    width:140px;
    float:left;
}
#input_box .small{
    color:#399c26;
    display:block;
    font-size:11px;
    font-weight:normal;
    text-align:right;
    width:140px;
}
#input_box input{
    float:left;
    font-size:12px;
    padding:4px 2px;
    border:solid 1px #399c26;
    width:200px;
    margin:2px 0 20px 10px;
}
#input_box textarea{
    float:left;
    font-size:12px;
    padding:4px 2px;
    border:solid 1px #399c26;
    width:200px;
 height:150px;
    margin:2px 0 20px 10px;
}
#input_box button{
    clear:both;
    margin-left:150px;
    width:125px;
    height:31px;
    background:#399c26;
    text-align:center;
    line-height:31px;
    color:#ffffff;
    font-size:11px;
    font-weight:bold;
}
-->


</style>

<script type="text/网页特效" src="ajax.网页特效"></script>

</head>

<body>
<div class="main">
  <div class="header">
    <h1>欢迎使用anfms</h1>
    <p><span>v0.1.0 alpha</span></p>
  </div>
  <div class="button_list">
    <div class="button"><a href="网页特效:void(0);" onclick="javascript:file_list('/');document.getelementbyid('file_list').style.display='block';document.getelementbyid('file_list_close').style.display='block';document.getelementbyid('fade').style.display='block'">文件列表</a></div>
    <div class="button"><a href="javascript:void(0);" onclick="javascript:file_list('/');document.getelementbyid('file_make').style.display='block';document.getelementbyid('file_list_close').style.display='block';document.getelementbyid('fade').style.display='block'">创建文件</a></div>
    <div class="button"><a href="javascript:void(0);" onclick="document.getelementbyid('file_content_input').style.display='block';document.getelementbyid('file_list_close').style.display='block';document.getelementbyid('fade').style.display='block'">写入内容</a></div>
    <div class="button"><a href="javascript:void(0);" onclick="document.getelementbyid('file_get_content').style.display='block';document.getelementbyid('file_list_close').style.display='block';document.getelementbyid('fade').style.display='block'">读取内容</a></div>
  </div>
  <div class="footer">
     <p>
     ©2009-2010 uniter.com.cn powered by uniter team<br />
     supported by ie7+ firefox opera
     </p>    
  </div>
 
  <div class="file_list_close" id="file_list_close">
    <a href="javascript:void(0);" onclick="document.getelementbyid('file_list').style.display='none';document.getelementbyid('file_content_input').style.display='none';document.getelementbyid('file_get_content').style.display='none';document.getelementbyid('file_list_close').style.display='none';document.getelementbyid('fade').style.display='none';location.reload()">x</a>
  </div>
  <div class="file_list" id="file_list"></div>
  <div class="file_list" id="file_content_input">
    <div id="input_box" class="file_content_input_form">
    <form id="form" name="form" method="post" action="">
        <label>文件名
           <span class="small">例如:abc.txt</span>
       </label>
       <input type="text" name="input_name" id="input_name" />
       <label>内容值
            <span class="small">添加文件内容</span>
       </label>
       <textarea name="input_content" id="input_content"></textarea>
       <button type="button" onclick="javascript:file_content_input('input_name='+document.getelementbyid('input_name').value+'&input_content='+document.getelementbyid('input_content').value);">确定</button>
       <div class="spacer"></div>
    </form>
    </div>
  </div>
  <div class="file_list" id="file_get_content">
    <div id="input_box" class="file_content_input_form">
    <form id="get_form" name="get_form" method="post" action="">
        <label>文件名
           <span class="small">例如:abc.txt</span>
       </label>
       <input type="text" name="input_name" id="input_name" />      
       <button type="button" onclick="javascript:file_get_content('input_name='+document.get_form.input_name.value);">确定</button>
       <div class="spacer"></div>
    </form>
    </div>
  </div>
  <div class="file_list" id="file_make">
    <div id="input_box" class="file_content_input_form">
    <form id="make_form" name="make_form" method="post" action="">
        <label>文件夹
           <span class="small">例如:abc</span>
       </label>
       <input type="text" name="input_name" id="input_name" />      
       <button type="button" onclick="javascript:file_make('input_name='+document.make_form.input_name.value);">确定</button>
       <div class="spacer"></div>
    </form>
    </div>
  </div>
  <div id="fade" class="black_overlay"></div>
</div>
</body>
</html>

ajax.网页特效

<script>
// javascript document
function do_ajax() {   
}
function ajax_get_data(id) { 
}
function file_list(path) { 
    var xhr;
 if (window.xmlhttprequest) {
  xhr = new xmlhttprequest();
 }else if (window.activexobject) {
  xhr = new activexobject("msxml2.xmlhttp");
 }else {
  throw new error("ajax is not supported by this www.zzzyk.com browser");
 }
 xhr.onreadystatechange = function () {
  if (xhr.readystate == 4) {
   if (xhr.status >= 200 && xhr.status < 300) {
    document.getelementbyid('file_list').innerhtml = xhr.responsetext;
   }
  }
 }
 xhr.open('get','anfms.php?action=filelist&path='+path);
 xhr.setrequestheader("if-modified-since","0");
 xhr.send();
}
function file_content_input(data) {
 var data;
 var xhr;
 if (window.xmlhttprequest) {
  xhr = new xmlhttprequest();
 }else if (window.activexobject) {
  xhr = new activexobject("msxml2.xmlhttp");
 }else {
  throw new error("ajax is not supported by this browser");
 }
 xhr.onreadystatechange = function () {
  if (xhr.readystate == 4) {
   if (xhr.status >= 200 && xhr.status < 300) {
    document.getelementbyid('file_content_input').innerhtml = xhr.responsetext;
   }
  }
 }
 xhr.open('get','anfms.php?action=inputcontent&'+data);
 xhr.setrequestheader("if-modified-since","0");
 xhr.send(); 
}
function file_get_content(data) {
 var data;
 var xhr;
 if (window.xmlhttprequest) {
  xhr = new xmlhttprequest();
 }else if (window.activexobject) {
  xhr = new activexobject("msxml2.xmlhttp");
 }else {
  throw new error("ajax is not supported by this browser");
 }
 xhr.onreadystatechange = function () {
  if (xhr.readystate == 4) {
   if (xhr.status >= 200 && xhr.status < 300) {
    document.getelementbyid('file_get_content').innerhtml = xhr.responsetext;
   }
  }
 }
 xhr.open('get','anfms.php?action=getcontent&'+data);
 xhr.setrequestheader("if-modified-since","0");
 xhr.send(); 
}
function file_make(file) {
 var file;
 var xhr;
 if (window.xmlhttprequest) {
  xhr = new xmlhttprequest();
 }else if (window.activexobject) {
  xhr = new activexobject("msxml2.xmlhttp");
 }else {
  throw new error("ajax is not supported by this browser");
 }
 xhr.onreadystatechange = function () {
  if (xhr.readystate == 4) {
   if (xhr.status >= 200 && xhr.status < 300) {
    document.getelementbyid('file_make').innerhtml = xhr.responsetext;
   }
  }
 }
 xhr.open('get','anfms.php?action=filemake&'+file);
 xhr.setrequestheader("if-modified-since","0");
 xhr.send(); 
}
function file_delete(file) {
 var file;
 var xhr;
 if (window.xmlhttprequest) {
  xhr = new xmlhttprequest();
 }else if (window.activexobject) {
  xhr = new activexobject("msxml2.xmlhttp");
 }else {
  throw new error("ajax is not supported by this browser");
 }
 xhr.onreadystatechange = function () {
  if (xhr.readystate == 4) {
   if (xhr.status >= 200 && xhr.status < 300) {
    document.getelementbyid('file_list').innerhtml = xhr.responsetext;
   }
  }
 }
 xhr.open('get','anfms.php?action=delete_file&file='+file);
 xhr.setrequestheader("if-modified-since","0");
 xhr.send(); 
}
function dir_delete(file) {
 var file;
 var xhr;
 if (window.xmlhttprequest) {
  xhr = new xmlhttprequest();
 }else if (window.activexobject) {
  xhr = new activexobject("msxml2.xmlhttp");
 }else {
  throw new error("ajax is not supported by this browser");
 }
 xhr.onreadystatechange = function () {
  if (xhr.readystate == 4) {
   if (xhr.status >= 200 && xhr.status < 300) {
    document.getelementbyid('file_list').innerhtml = xhr.responsetext;
   }
  }
 }
 xhr.open('get','anfms.php?action=delete_dir&file='+file);
 xhr.setrequestheader("if-modified-since","0");
 xhr.send(); 
}
</script>

anfms.php

<?php
/**
 * all needs file e zzzyk.com manage system
 * this is a test code
 * return data to ajax
 * by aiens
 * 2010-01-13 19:20
 */

header("content-type: text/html; charset=utf-8");
$document_root = $_server['document_root'];
if ($_get['action'] == 'filelist') { //return file list
 echo '<div class="file_list_t">'; 
 $checkpath = str_replace($document_root,'',$_get['path']);
 if ($checkpath != '/../') {  
  $checkpath = str_replace('../','',$checkpath);
  $path1 = $document_root.str_replace($document_root,'',$_get['path']);
  $dir = opendir($path1);  
  while ($file = readdir($dir)) {
     if (!($file == '..')) {   
     if ($file == '.') {
      $path = $path1.'../';     
      $file = '返回';  
      if ($checkpath != '/') {
       echo '<li><a href="javascript:void(0);" onclick="javascript:file_list(''.$path.'');">'.$file.'</a></li>';
      }
     }else{     
      $path = $path1.$file.'/';     
      $path = iconv('gb2312','utf-8',$path);
      if (is_dir($path1.$file)) {
         echo '<li><a href="javascript:void(0);" onclick="javascript:file_list(''.$path.'');">'.$file.'</a><span><a href="javascript:void(0);" onclick="javascript:if(confirm('确认删除?')) dir_delete(''.$path.'');">删除</a></span></li>';
      }else{
      $file = iconv('gb2312','utf-8',$file);
      $filesize = filesize($path1.$file)/1000;
      echo '<li>'.$file.' - ('.$filesize.'kb)<span><a href="javascript:void(0);" onclick="javascript:if(confirm('确认删除?')) file_delete(''.$path1.$file.'');">删除</a></span></li>';
      }
     }
     }    
  }
  closedir($dir);
 }
 echo '</div>';
}else if ($_get['action'] == 'inputcontent') { //input data to file
 $name = $_get['input_name'];
 $content = $_get['input_content'];
 $data = date('y-m-d h:i:s');
 $fp = @fopen(dirname(__file__).'/'.$name, 'ab');
 $data = "data:".$data."t content:".$content."n";
 $fw = @fwrite($fp, $data, strlen($data));
 @fclose($fp);
 if ($fw) {
  echo '成功写入';
 }else{
  echo '写入失败';
 }
}else if ($_get['action'] == 'getcontent') { //get data to file
 $name = $_get['input_name'];
 $fp = @fopen(dirname(__file__).'/'.$name, 'rb');
 if (!$fp) {
  echo 'www.zzzyk.com 系统所在目录不存在此文件,无法读取';
  exit;
 }
 while (!feof($fp)) {
  $content = fgets($fp);
  echo $content.'<br />';
 }
}else if ($_get['action'] == 'filemake') { //make dir
 $name = $_get['input_name'];
 function makedir($path) { 
      if (!file_exists($path)) {
        makedir(dirname($path)); 
        return mkdir($path, 0777);   
      } 
    }
 if (makedir($name)) {
  echo '成功创建';
 }else{
  echo '创建失败';
 }
}else if ($_get['action'] == 'delete_file') { //delete file
    $file = $_get['file'];
 unlink($file);
 echo '成功删除';
}else if ($_get['action'] == 'delete_dir') { //delete dir
 $dir = $_get['file'];
 function delete_dir($dir) {
   if(! is_dir($dir)) {
    return false;
   }
    $handle = @opendir($dir);
    while(($file = @readdir($handle)) !== false) {
     if($file != '.' && $file != '..') {
      $dir = $dir . '/' . $file;
      is_dir($dir) ? delete_dir($dir) : @unlink($dir);
     }
    }
   closedir($handle);
   return rmdir($dir) ;
 }
 if (delete_dir($dir)) {
  echo '成功删除';
 }else{
  echo '删除失败';
 } 
}
?>

补充:Php教程,Php高级应用 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,