1. 云栖社区>
  2. PHP教程>
  3. 正文

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

作者:用户 来源:互联网 时间:2017-12-01 14:47:55

源代码文件管理

php文件管理源代码(1/2) - 摘要: 本文讲的是php文件管理源代码(1/2),php文件管理源代码//这款文件管理源码是一款可以创建,删除文件,上传文件,设置文件权限,获取磁盘剩余空间,文件复制,修改名称,目录管理,等。php教程文件管理源代码//这款文件管理源码是一款可以创建,删除文件,上传文件,设置文件权限,获取

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 ([email protected]_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+'nplease 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+'nplease 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+'nplease 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).' free:'.realsize($drive->freespace).' 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

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索源代码 文件管理 ,以便于您获取更多的相关知识。