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) ? '成功' : '失敗');
}
}?>
<!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