php檔案管理原始碼(1/2)

來源:互聯網
上載者:User

/*************************************
*---檔案管理
*作者:菜根
*聲明:此檔案功能參照於phps教程py,
************************************/
 

 代碼如下 複製代碼

 if(function_exists('date_default_timezone_set'))
 {
  @date_default_timezone_set('asia/shanghai');
 }
 
 define('is_win', directory_separator == '\');
 define('is_com', class_exists('com') ? 1 : 0 );
 define('web_root', str_replace('\', '/', dirname(__file__)).'/');
 $phpself = htmlspecialchars($_server['php_self'] ? $_server['php_self'] : $_server['script_name']);
 $msg = '';
 
 if(!empty($_post['cwddir']))
 {
  $_post['cwddir'] = addslashes($_post['cwddir']);
   if(is_dir($_post['cwddir']))
    chdir($_post['cwddir']);
 }
 
 $cwddir = getcwddir();//擷取當前工作路徑
 
 $freespaces = disk_free_space($cwddir);//當前磁碟剩餘空間
 $totalspaces = disk_total_space($cwddir);//當前總空間大小
 //header("content-type: text/html; charset=utf-8");
 
 //建立目錄
 if($_post['newdirname'])
 {
  $newdir = $_post['dirname'].$_post['newdirname'];
  if(file_exists($newdir))
  {
   $msg = "該目錄:$newdir 已存在,請用其它名字";
  }else{
   $msg = "建立$newdir".(@mkdir($newdir,0777) ? '成功' : '失敗');
  }
 }
 
 //檔案上傳
 if($_post['upload'])
 {
  $fname = $_files['upfilename'];
  $msg = fileupload($fname,$_post['todir']);
 }
 
 //更改名稱
 if($_post['newname'])
 {
  $newname = addslashes($_post['newname']);
  $oldname = addslashes($_post['oldname']);
  $dirname = addslashes($_post['dirname']);
  $newname = $dirname.$newname;
  if(!file_exists($newname))
  {
   if(rename($oldname,$newname))
   {
    $msg = '更改名稱成功';
   }else{
    $msg = '無法更改,請檢查相關配置和許可權';
   }
  }else{
   $msg = '檔案已存在,請更換其它名稱!';
  }
 }
 
 //複製檔案
 if($_post['tofile'])
 {
  $tofile = addslashes($_post['tofile']);
  $oldname = addslashes($_post['oldname']);
  if(file_exists($tofile))
  {
   $msg = '檔案已存在,請更換其它名稱!';
  }else{
   if(copy($oldname,$tofile))
   {
    $msg = "複製檔案 $oldname 到 $tofile 成功!";
   }else{
    $msg = "複製檔案 $oldname 到 $tofile 失敗,請檢查相關配置和許可權!";
   }
  }
 }
 
 //下載檔案:源碼來自phpspy2008,因為我對header()一竅不通,呵呵
 if($_post['dfile'])
 {
  $thefile = addslashes($_post['dfile']);
  //header("location:$dfile");
  if (!@file_exists($thefile)) {
   $errmsg = 'the file you want downloadable was nonexistent';
  } else {
   $fileinfo = pathinfo($thefile);
   header('content-type: application/x-'.$fileinfo['extension']);
   header('content-disposition: attachment; filename='.$fileinfo['basename']);
   header('content-length: '.filesize($thefile));
   @readfile($thefile);
   exit;
  }
 }
 
 //刪除單個檔案
 if($_post['delfilename'])
 {
  $delfilename = addslashes($_post['delfilename']);
  if(!file_exists($delfilename))
  {
   $msg = '檔案不存在!';
  }else{
   if(unlink($delfilename))
   {
    $msg = '刪除'.$delfilename.'成功';
   }else{
    $msg = '刪除'.$delfilename.'失敗,請檢查相關配置和許可權';
   }
  }
 }
 
 //刪除檔案夾
 if($_post['deldirname'])
 {
  $delname = addslashes($_post['deldirname']);
  if(deltree($delname))
   {
    $msg = '刪除'.$delname.'成功';
   }else{
    $msg = '刪除'.$delname.'失敗,請檢查相關配置和許可權';
   }
 }
 
 //建立檔案
 if($_post['createtofile'])
 {
  $createfilename = htmlspecialchars(addslashes($_post['createdirname'].$_post['createtofile']));
  //echo $createfilename;
  if(file_exists($createfilename))
  {
   $msg = '檔案已存在!';
   $createfilename = 0;
  }else{
   if($creatfhandle = fopen($createfilename,'w'))
   {
    //echo 'dffdfd';
    fclose($creatfhandle);
   }else {
    $msg = '檔案建立失敗,請檢查相關配置和許可權!';
   }
  }
 }
 
 //編輯檔案
 if($_post['editfilename'])
 {
  $createfilename = addslashes($_post['editfilename']);
  if(!file_exists($createfilename))
  {
   $msg = '檔案不存在!';
   $createfilename = 0;
  }
 }
 
 //儲存編輯過的檔案
 if($_post['editsubmit'])
 {
  $savefilecontent = htmlspecialchars($_post['fcontent']);
  $savefilename = $_post['editfname'];
  if($fhandle = fopen($savefilename,'wb'))
  {
   if(fwrite($fhandle,$savefilecontent))
   {
    $msg = '編輯成功!';
   }else{
    $msg = '編輯失敗!';
   }
   fclose($fhandle);
  }
 }
 
 //修改屬性
 if ($_post['newperm'])
 {
  $newperm = $_post['newperm'];
  $pfile = addslashes($_post['pfile']);
  if (!file_exists($pfile))
  {
   $msg = '檔案不存在';
  } else {
   $newperm = base_convert($newperm,8,10);
   $msg = '修改檔案屬性 '.(@chmod($pfile,$newperm) ? '成功' : '失敗');
  }
 }

?>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>檔案管理</title>
<style type="text/css教程">
div {
 width: 1000px;
 text-align: left;
 border: 1px solid #ccffff;
 margin: 1px;
 padding: 1px;
 display: block;
 float: left;
 background-color: #fafafb;
}
body {
 font-size: 12px;
 line-height: 16px;
 font-family: arial, helvetica, sans-serif;
 text-align: left;
}
</style>
<script type="text/網頁特效">
//這部分網頁特效代碼來自phpspy,我不太懂網頁特效,哈哈
function checkall(form) {
 for(var i=0;i<form.elements.length;i++) {
  var e = form.elements[i];
  if (e.name != 'chkall')
  e.checked = form.chkall.checked;
    }
}
function $(id) {
 return document.getelementbyid(id);
}
function goaction(act){
 $('goaction').action.value=act;
 $('goaction').submit();
}
function godir(dir){
 $('godir').cwddir.value=dir;
 $('godir').submit();
}
function createdir(){
 var newdirname;
 newdirname = prompt('請輸入目錄名(注意:只能在目前的目錄下建立):', '');
 if (!newdirname) return;
 $('createdir').newdirname.value=newdirname;
 $('createdir').submit();
}
function changperm(pfile){
 var newperm;
 newperm = prompt('current file:'+pfile+' please input new attribute:', '');
 if (!newperm) return;
 $('fileperm').newperm.value=newperm;
 $('fileperm').pfile.value=pfile;
 $('fileperm').submit();
}
function copyfile(sname){
 var tofile;
 tofile = prompt('original file:'+sname+' please input object file (fullpath):', '');
 if (!tofile) return;
 $('copyfile').tofile.value=tofile;
 $('copyfile').oldname.value=sname;
 $('copyfile').submit();
}
function rename(oldname){
 var newfilename;
 newfilename = prompt('former file name:'+oldname+' please input new filename:', '');
 if (!newfilename) return;
 $('rename').newname.value=newfilename;
 $('rename').oldname.value=oldname;
 $('rename').submit();
}
function delsinglefile(thefile){
 if(thefile && !confirm('確定要刪除'+thefile+'嗎?')){
  return;
 }
 $('delsfile').delfilename.value=thefile;
 $('delsfile').submit();
}
function deldir(thefile){
 if(thefile && !confirm('確定要刪除'+thefile+'及其子檔案嗎?')){
  return;
 }
 $('deldirs').deldirname.value=thefile;
 //confirm('確定要刪除'+thefile+'嗎?');
 $('deldirs').submit();
}
function downfile(thefile){
 if(!thefile){
  return;
 }
 $('downfile').dfile.value=thefile;
 $('downfile').submit();
}
function createfile(nowpath){
 var filename;
 filename = prompt('please input the file name:', '');
 if (!filename) return;
 //opfile('editfile',nowpath + filename,nowpath);
 //confirm('確定要建立'+filename+'嗎?');
 $('createfiles').createtofile.value=filename;
 //confirm('確定要建立'+filename+'嗎?');
 $('createfiles').submit();
}
function editfile(filename){
 if (!filename) return;
 $('editfiles').editfilename.value=filename;
 
 $('editfiles').submit();
}
</script>
</head>
<body>
<!--
<div>
  <strong style="float:left">127.0.0.1(localhost)</strong>
  <span style="float:right">access time:<?php echo date('y-m-d h:m:s'); ?></span>
</div>
-->
<div>
  <a href="網頁特效:godir('<?php echo addslashes($_server['document_root']); ?>');">webroot</a>
  |
  <a href="javascript:createdir();">create directory</a>
  |
  <a href="javascript:createfile('c:/inetpub/wwwroot/mywork/');">create file</a>
  <?php
  if (is_win && is_com) {
  //此代碼來源於phpspy
  $obj = new com('scripting.filesystemobject');
  if ($obj && is_object($obj)) {
   $drivetypedb = array(0 => 'unknow',1 => 'removable',2 => 'fixed',3 => 'network',4 => 'cdrom',5 => 'ram disk');
   foreach($obj->drives as $drive) {
    if ($drive->drivetype == 2) {
     echo(' | <a href="javascript:godir(''.$drive->path.'/');" title="size:'.realsize($drive->totalsize).'&#13;free:'.realsize($drive->freespace).'&#13;type:'.$drivetypedb[$drive->drivetype].'">'.$drivetypedb[$drive->drivetype].'('.$drive->path.')</a>');
    } else {
     echo(' | <a href="javascript:godir(''.$drive->path.'/');" title="type:'.$drivetypedb[$drive->drivetype].'">'.$drivetypedb[$drive->drivetype].'('.$drive->path.')</a>');
    }
   }
  }
 }
?>
 
</div>
<div>
  <strong><?php echo $msg; ?></strong>
</div>
<div>
  <form id="godir" name="godir" method="post" action="<?php echo $phpself; ?>">
    目前的目錄: (<?php echo getpermsnum($cwddir); echo is_writable($cwddir) ? ' 可寫' : ' 不可寫'; ?>)
    <input name="cwddir" type="text" id="cwddir" style="width:50%" value="<?php echo $cwddir; ?>" />
    <input type="submit" name="godir" id="godir" value="提交" />
  </form>
</div>
<div>
  <form action="" method="post" enctype="multipart/form-data" name="form2" id="form2">
    <strong>file manager - current disk free <?php echo realsize($freespaces); ?> of <?php echo realsize($totalspaces); ?> (<?php echo round($freespaces/$totalspaces*100 , 2).'%';?>) </strong>
    <input name="upfilename" type="file" id="upfilename" />
    <input name="todir" type="hidden" value="<?php echo $cwddir; ?>" />
    <input type="submit" name="upload" id="upload" value="上傳" />
  </form>
</div>
<div>
  <form id="createdir" name="createdir" method="post" action="<?php echo $phpself; ?>">
    <input name="newdirname" type="hidden" id="newdirname" value="" />
    <input name="dirname" type="hidden" id="dirname" value="<?php echo $cwddir; ?>" />
  </form>
  <!--<form id="createfile" name="createfile" method="post" action="<?php echo $phpself; ?>">
    <input name="newfilename" type="hidden" id="newfilename" value="" />
    <input name="dirname" type="hidden" id="dirname" value="<?php echo $cwddir; ?>" />
  </form> -->
  <form id="deldir" name="deldir" method="post" action="<?php echo $phpself; ?>">
    <input name="newdirname" type="hidden" id="newdirname" value="" />
    <input name="dirname" type="hidden" id="dirname" value="<?php echo $cwddir; ?>" />
  </form>
  <form id="rename" name="rename" method="post" action="<?php echo $phpself; ?>">
    <input name="newname" type="hidden" id="newname" value="" />
    <input name="oldname" type="hidden" id="oldname" value="" />
    <input name="dirname" type="hidden" id="dirname" value="<?php echo $cwddir; ?>" />
  </form>
  <form id="copyfile" name="copyfile" method="post" action="<?php echo $phpself; ?>">
    <input name="tofile" type="hidden" id="tofile" value="" />
    <input name="oldname" type="hidden" id="oldname" value="" />
  </form>
  <form id="downfile" name="downfile" method="post" action="<?php echo $phpself; ?>">
    <input name="dfile" type="hidden" id="dfile" value="" />
  </form>
  <form id="delsfile" name="delsfile" method="post" action="<?php echo $phpself; ?>">
    <input name="delfilename" type="hidden" id="delfilename" value="" />
  </form>
  <form id="deldirs" name="deldirs" method="post" action="<?php echo $phpself; ?>">
    <input name="deldirname" type="hidden" id="deldirname" value="" />
  </form>
  <form id="createfiles" name="createfiles" method="post" action="<?php echo $phpself; ?>">
    <input name="createtofile" type="hidden" id="createtofile" value="" />
    <input name="createdirname" type="hidden" id="createdirname" value="<?php echo $cwddir; ?>" />
  </form>
  <form id="editfiles" name="editfiles" method="post" action="<?php echo $phpself; ?>">
    <input name="editfilename" type="hidden" id="editfilename" value="" />
  </form>
  <form id="fileperm" name="fileperm" method="post" action="<?php echo $phpself; ?>">
    <input name="newperm" type="hidden" id="newperm" value="" />
    <input name="pfile" type="hidden" id="pfile" value="" />
  </form>
 

 

首頁 1 2 末頁
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.