Home  >  Article  >  Backend Development  >  php文件管理源代码(1/2)_PHP教程

php文件管理源代码(1/2)_PHP教程

WBOY
WBOYOriginal
2016-07-20 11:08:241492browse

php文件管理源代码//这款文件管理源码是一款可以创建,删除文件,上传文件,设置文件权限,获取磁盘剩余空间,文件复制,修改名称,目录管理,等。

php教程文件管理源代码
//这款文件管理源码是一款可以创建,删除文件,上传文件,设置文件权限,获取磁盘剩余空间,文件复制,修改名称,目录管理,等。
/*************************************
*---文件管理
*作者:菜根
*主页:http://www.phpvc.com/blog
*声明:此文件功能参照于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) ? '成功' : '失败');
  }
 }

?>




Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn