修改代碼
if (! (isset ( $arr ['file'] )) && ! (isset ( $arr ['dir'] ))) {
echo "<script>alert('沒有文件或目錄檐盟!');location.href='index.php';</script>";
}
$arr=array();
if (isset ( $arr ) && isset ( $arr ['file'] )) {
if (isset ( $arr ['dir'] )) {
if ($arr ['dir']) {
<a href="index.php?path=<?php echo $p;?>"><img class="small"
src="images/show.png" alt="" title="查看" /></a>
dir.func.php
<?php
/**
* 遍歷目錄函數(shù) 最外層
* @param string $path
* @return array
*/
function readDirectory($path) {
$handle = opendir ( $path );
$arr=array();
// !== 防止文件名為0 被忽略
while ( ($item = readdir ( $handle )) !== false ) {
if ($item != "." && $item != "..") {
if (is_file ( $path . "/" . $item )) {
$arr ['file'] [] = $item;
}
if (is_dir ( $path . "/" . $item )) {
$arr ['dir'] [] = $item;
}
}
}
closedir ( $handle );
return $arr;
}
// $path = "file";
// print_r ( readDirectory ( $path ) );
/**
* 得到文件夾大小
* @param unknown $path
* @return Ambigous <unknown, number>
*/
function dirSize($path){
$sum=0;
global $sum;
$handle=opendir($path);
while (($item=readdir($handle))!==false){
if($item!="."&&$item!=".."){
if(is_file($path."/".$item)){
$sum+=filesize($path."/".$item);
}
if(is_dir($path."/".$item)){
$func=__FUNCTION__;
$func($path."/".$item);//遞歸
}
}
}
closedir($handle);
return $sum;
}
?>
index.php
<?php
require_once ('dir.func.php');
require_once 'file.func.php';
require_once 'common.func.php';
$path = 'file';
$path = isset ( $_REQUEST ['path'] ) ? $_REQUEST ['path'] : $path;
$arr = readDirectory ( $path );
$act = isset ( $_REQUEST ['act'] ) ? $_REQUEST ['act'] : '';
$filename = isset ( $_REQUEST ['filename'] ) ? $_REQUEST ['filename'] : '?';
$redirect = "index.php?path={$path}";
// echo $act."<br/>";
// echo $filename;
if (! (isset ( $arr ['file'] )) && ! (isset ( $arr ['dir'] ))) {
echo "<script>alert('沒有文件或目錄东亦!');location.href='index.php';</script>";
}
if ($act == '創(chuàng)建文件') {
$mes = createFile ( $path . "/" . $filename );
alertMes ( $mes, $redirect );
} else if ($act == 'showContent') {
// 查看文件內(nèi)容
$content = file_get_contents ( $filename );
if (strlen ( $content )) {
// textarea能完整顯示代碼
// echo "<textarea col='1000' rows='10'>$content</textarea>";
// 高亮顯示字符創(chuàng)中的PHP代碼 true不自動輸出(echo)
$newContent = highlight_string ( $content, true );
// 高亮顯示文件中的PHP代碼
// $content=highlight_file($filename,true);
$str = <<<EOF
<table width='100%' bgcolor='pink' cellpadding='5' cellspacing='0'>
<tr>
<td>
{$newContent}
</td>
</tr>
</table>
EOF;
echo $str;
} else {
alertMes ( "文件沒有內(nèi)容,請編輯再查看!", $redirect );
}
} else if ($act == 'editContent') {
$content = file_get_contents ( $filename );
$str = <<<EOF
<form action='index.php?act=doEdit' method='post'>
<textarea name='content' col='190' rows='10'>{$content}</textarea>
<input type='hidden' name='filename' value='{$filename}'/>
<input type="submit" value="修改文件內(nèi)容"/>
</form>
EOF;
echo $str;
} else if ($act == "doEdit") {
// 修改文件內(nèi)容的操作
$content = $_REQUEST ['content'];
// echo $content;
if (file_put_contents ( $filename, $content )) {
$mes = "文件修改成功";
} else {
$mes = "文件修改失敗";
}
alertMes ( $mes, $redirect );
} else if ($act == "renameFile") {
// 完成重命名
$str = <<<EOF
<form action="index.php?act=doRename" method="post">
請?zhí)顚懶挛募?<input type="text" name="newname" placeholder="重命名"/>
<input type='hidden' name='filename' value='{$filename}'/>
<input type="submit" value="重命名"/>
</form>
EOF;
echo $str;
} else if ($act == 'doRename') {
// 實現(xiàn)重命名操作
$newname = $_REQUEST ['newname'];
$mes = renameFile ( $filename, $newname );
alertMes ( $mes, $redirect );
} else if ($act == 'delFile') {
$mes = deleteFile ( $filename );
alertMes ( $mes, $redirect );
} else if ($act == 'downFile') {
// 完成下載的操作
$mes = downFile ( $filename );
}
?>
<!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=UTF-8" />
<title>Insert title here</title>
<link rel="stylesheet" href="cikonss.css" />
<script src="jquery-ui/js/jquery-1.10.2.js"></script>
<script src="jquery-ui/js/jquery-ui-1.10.4.custom.js"></script>
<script src="jquery-ui/js/jquery-ui-1.10.4.custom.min.js"></script>
<link rel="stylesheet"
href="jquery-ui/css/ui-lightness/jquery-ui-1.10.4.custom.css"
type="text/css" />
<style type="text/css">
body,p,div,ul,ol,table,dl,dd,dt {
margin: 0;
padding: 0;
}
a {
text-decoration: none;
}
ul,li {
list-style: none;
float: left;
}
#top {
width: 100%;
height: 48px;
margin: 0 auto;
background: #E2E2E2;
}
#navi a {
display: block;
width: 48px;
height: 48px;
}
#main {
margin: 0 auto;
border: 2px solid #ABCDEF;
}
.small {
width: 25px;
height: 25px;
border: 0;
}
</style>
<script type="text/javascript">
function show(dis){
document.getElementById(dis).style.display="block";
}
function delFile(filename){
if(window.confirm("您確定要刪除嘛?刪除之后無法恢復(fù)喲!!!")){
location.href="index.php?act=delFile&filename="+filename;
}
return false;
}
function showDetail(t,filename){
$("#showImg").attr("src",filename);
$("#showDetail").dialog({
height:"auto",
width: "auto",
position: {my: "center", at: "center", collision:"fit"},
modal:false,//是否模式對話框
draggable:true,//是否允許拖拽
resizable:true,//是否允許拖動
title:t,//對話框標(biāo)題
show:"slide",
hide:"explode"
});
}
function goBack($back){
location.href="index.php?path="+$back;
}
</script>
</head>
<body>
<div id="showDetail" style="display: none">
<img src="" id="showImg" alt="" />
</div>
<h1>慕課網(wǎng)-在線文件管理器</h1>
<div id="top">
<ul id="navi">
<li><a href="index.php" title="主目錄"><span
style="margin-left: 8px; margin-top: 0px; top: 4px;"
class="icon icon-small icon-square"><span class="icon-home"></span></span></a></li>
<li><a href="#" onclick="show('createFile')" title="新建文件"><span
style="margin-left: 8px; margin-top: 0px; top: 4px;"
class="icon icon-small icon-square"><span class="icon-file"></span></span></a></li>
<li><a href="#" onclick="show('createFolder')" title="新建文件夾"><span
style="margin-left: 8px; margin-top: 0px; top: 4px;"
class="icon icon-small icon-square"><span class="icon-folder"></span></span></a></li>
<li><a href="#" onclick="show('uploadFile')" title="上傳文件"><span
style="margin-left: 8px; margin-top: 0px; top: 4px;"
class="icon icon-small icon-square"><span class="icon-upload"></span></span></a></li>
<li><a href="#" title="返回上級目錄" onclick="goBack('')"><span
style="margin-left: 8px; margin-top: 0px; top: 4px;"
class="icon icon-small icon-square"><span class="icon-arrowLeft"></span></span></a></li>
</ul>
</div>
<form action="index.php" method="post" enctype="multipart/form-data">
<table width="100%" border="1" cellpadding="5" cellspacing="0"
bgcolor="#ABCDEF" align="center">
<tr id="createFolder" style="display: none;">
<td>請輸入文件夾名稱</td>
<td><input type="text" name="dirname" /> <input type="hidden"
name="path" value="<?php echo $path;?>" /> <input type="submit"
name="act" value="創(chuàng)建文件夾" /></td>
</tr>
<tr id="createFile" style="display: none;">
<td>請輸入文件名稱</td>
<td><input type="text" name="filename" /> <input type="hidden"
name="path" value="<?php echo $path;?>" /> <input type="submit"
name="act" value="創(chuàng)建文件" /></td>
</tr>
<tr id="uploadFile" style="display: none;">
<td>請選擇要上傳的文件</td>
<td><input type="file" name="myFile" /> <input type="submit"
name="act" value="上傳文件" /></td>
</tr>
<tr>
<td>編號</td>
<td>名稱</td>
<td>類型</td>
<td>大小</td>
<td>可讀</td>
<td>可寫</td>
<td>可執(zhí)行</td>
<td>創(chuàng)建時間</td>
<td>修改時間</td>
<td>訪問時間</td>
<td>操作</td>
</tr>
<?php
if (isset ( $arr ) && isset ( $arr ['file'] )) {
if ($arr ['file']) {
$i = 1;
foreach ( $arr ['file'] as $val ) {
$p = $path . "/" . $val;
?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $val;?></td>
<td><?php $src=filetype($p)=='file'?"file_ico.png":"folder_ico.png";?>
<img src="images/<?php echo $src;?>" alt="" title="文件" /></td>
<td><?php echo transByte(filesize($p));?></td>
<td><?php $src=is_readable($p)?"correct.png":"error.png"?>
<img src="images/<?php echo $src?>" alt="" class="small" /></td>
<td><?php $src=is_writeable($p)?"correct.png":"error.png"?>
<img src="images/<?php echo $src?>" alt="" class="small" /></td>
<td><img
src="images/<?php echo (is_executable($p)? "correct.png":"error.png");?>"
alt="" class="small" /></td>
<td>
<?php echo date("Y-m-d H:i:s",filectime($p))?>
</td>
<td>
<?php echo date("Y-m-d H:i:s",filemtime($p))?>
</td>
<td>
<?php echo date("Y-m-d H:i:s",fileatime($p))?>
</td>
<?php
// 得到文件擴展名
// explode 將字符串打散為數(shù)組
// end() 函數(shù)將內(nèi)部指針指向數(shù)組中的最后一個元素剖淀,并輸出
$ext = strtolower ( end ( explode ( ".", $val ) ) );
$imageExt = array (
"gif",
"jpeg",
"jpg",
"png"
);
// in_array() 函數(shù)搜索數(shù)組中是否存在指定的值。
if (in_array ( $ext, $imageExt )) {
?>
<td><a href="#"
onclick="showDetail('<?php echo $val;?>','<?php echo $p;?>')"><img
class="small" src="images/show.png" alt="" title="查看" /></a>|
<?php
} else {
?>
<td><a href="index.php?act=showContent&filename=<?php echo $p?>"><img
class="small" src="images/show.png" alt="" title="查看" /></a>|
<?php
}
?>
<a href="index.php?act=editContent&filename=<?php echo $p?>"><img
class="small" src="images/edit.png" alt="" title="修改" /></a>| <a
href="index.php?act=renameFile&filename=<?php echo $p;?>"><img
class="small" src="images/rename.png" alt="" title="重命名" /></a>|
<a href=""><img class="small" src="images/copy.png" alt=""
title="復(fù)制" /></a>| <a href=""><img class="small"
src="images/cut.png" alt="" title="剪切" /></a>| <a href=""
onclick="return delFile('<?php echo $p;?>')"><img class="small"
src="images/delete.png" alt="" title="刪除" /></a>| <a
href="index.php?act=downFile&filename=<?php echo $p?>"><img
class="small" src="images/download.png" alt="" title="下載" /></a></td>
</tr>
<?php
$i ++;
}
} else {
// echo "<script type='text/javascript'>alert('沒有文件蘸朋!');</script>";
}
}
?>
<!-- 讀取目錄的操作 -->
<?php
if (isset ( $arr ['dir'] )) {
if ($arr ['dir']) {
foreach ( $arr ['dir'] as $val ) {
$p = $path . "/" . $val;
?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $val;?></td>
<td><?php $src=filetype($p)=='file'?"file_ico.png":"folder_ico.png";?>
<img src="images/<?php echo $src;?>" alt="" title="文件" /></td>
<td><?php $sum=0;echo transByte(dirSize($p));?></td>
<td><?php $src=is_readable($p)?"correct.png":"error.png"?>
<img src="images/<?php echo $src?>" alt="" class="small" /></td>
<td><?php $src=is_writeable($p)?"correct.png":"error.png"?>
<img src="images/<?php echo $src?>" alt="" class="small" /></td>
<td><img
src="images/<?php echo (is_executable($p)? "correct.png":"error.png");?>"
alt="" class="small" /></td>
<td>
<?php echo date("Y-m-d H:i:s",filectime($p))?>
</td>
<td>
<?php echo date("Y-m-d H:i:s",filemtime($p))?>
</td>
<td>
<?php echo date("Y-m-d H:i:s",fileatime($p))?>
</td>
<td><a href="index.php?path=<?php echo $p;?>"><img class="small"
src="images/show.png" alt="" title="查看" /></a>| <a
href="index.php?act=renameFile&filename=<?php echo $p;?>"><img
class="small" src="images/rename.png" alt="" title="重命名" /></a>| <a
href="">![](images/copy.png)</a>|
<a href=""><img class="small" src="images/cut.png" alt=""
title="剪切" /></a>| <a href=""
onclick="return delFile('<?php echo $p;?>')"><img class="small"
src="images/delete.png" alt="" title="刪除" /></a>| <a
href="index.php?act=downFile&filename=<?php echo $p?>"><img
class="small" src="images/download.png" alt="" title="下載" /></a></td>
</tr>
<?php
$i ++;
}
} else {
// echo "<script type='text/javascript'>alert('沒有目錄!');</script>";
}
}
?>
</table>
</form>
</body>
</html>