程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php實現的遍歷文件夾下所有文件,編輯刪除

php實現的遍歷文件夾下所有文件,編輯刪除

編輯:PHP綜合
復制代碼 代碼如下:
<?php
/*
遍歷文件夾; 功能:(a)可刪除文件 (b)可編輯文本,網頁文件 (c)可刪除文件夾,前提是該文件夾為空 (d)可建立文件,文件夾,修改文件夾名稱;
lost63.com原創, 轉載請注明出處. 交流QQ:35501547
*/
$path=$DOCUMENT_ROOT;
$path=str_replace("/","\\",$path);
//指定文件夾
$path=$path."\\";
if($_GET['folder'])
{
$path.=$_GET['folder']."\\";
}
//本頁面路徑,下面傳回時將用到;
$url=$_SERVER['PHP_SELF'];
//如果是文件夾,將加上鏈接;
function folder($path,$str)
{
if(filetype($path.$str)=="dir")
{
return "<a href=\"?folder=".$_GET['folder']."\\".$str."\">$str</a>";
}else{
return $str;
}
}
switch($_GET['action'])
{
case "del";
if($_GET['type']=="file")
{
unlink($_GET['path']);
}else{
rmdir($_GET['path']);
}
echo "<script type=\"text/javascript\">alert('恭喜,刪除成功!');location.href=\"".$url."\";</script>";
break;
case "edit";
if($_GET['type']=="file")
{
$file=fopen($_GET['path'],"r");
while(!feof($file))
{
$result.=fgets($file,9999);
}
fclose($file);
echo '<form name="form1" method="post" action="?action=editsave&path='.$_GET['path'].'&type='.$_GET['type'].'">
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="25" bgcolor="#99CC00">>><strong> 編輯文件</strong> > <a href="'.$url.'">返回</a></td>
</tr>
<tr>
<td height="25" align="left" bgcolor="#FFFFCC">->文件名:'.$_GET['path'].'</td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><textarea name="textarea" cols="135" rows="20">'.$result.'</textarea></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</table>
</form>';
}else{
echo '<form name="form1" method="post" action="?action=dir_rename&path='.$_GET['path'].'&type='.$_GET['type'].'">
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="25" bgcolor="#99CC00">>><strong> 新建文件</strong> > <a href="'.$url.'">返回</a></td>
</tr>
<tr>
<td height="25" align="left" bgcolor="#FFFFCC">->文件夾更名:
<input name="filename" type="text" value="'.$_GET['path'].'" size="50"></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</table>
</form>';
}
break;
case "editsave";
$file=fopen($_GET['path'],"w");
fwrite($file,$_POST['textarea']);
fclose($file);
echo "<script type=\"text/javascript\">alert('恭喜,編輯成功!');location.href=\"".$url."\";</script>";
break;
case "addfile";
echo '<form name="form1" method="post" action="?action=filesave&path='.$_GET['path'].'">
<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="25" bgcolor="#99CC00">>><strong> 新建文件</strong> > <a href="'.$url.'">返回</a></td>
</tr>
<tr>
<td height="25" align="left" bgcolor="#FFFFCC">->文件名:
<input name="filename" type="text" value="'.$_GET['path'].'" size="50"></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><textarea name="textarea" cols="135" rows="20">輸入內容
</textarea></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</table>
</form>';
break;
case "filesave";
//包含點則建立文件,否則建立文件夾
if(strpos($_POST['filename'],"."))
{
$file=fopen($_POST['filename'],"w");
fwrite($file,$_POST['textarea']);
fclose($file);
}else{
//文件夾若存在則退出,不存在則建立!
if(file_exists($_POST['filename']))
{
exit;
}else{
mkdir($_POST['filename']);
}
}
echo "<script type=\"text/javascript\">alert('恭喜,".$_POST['filename']."建立成功!');location.href=\"".$url."\";</script>";
break;
case "dir_rename";
rename($_GET['path'],$_POST['filename']);
echo "<script type=\"text/javascript\">alert('恭喜,".$_POST['filename']."改名成功!');location.href=\"".$url."\";</script>";
break;
default:
$s=explode("\n",trim(`dir/b/o:gn $path`));
echo '<table width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td height="25" colspan="4" bgcolor="#99CC00">>><strong> 文件管理</strong> > <a href="?action=addfile&path='.$path.'">建立文件</a> > <a href="'.$url.'">返回</a></td>
</tr>
<tr>
<td height="25" align="center" bgcolor="#FFFFCC">文件/文件夾</td>
<td align="center" bgcolor="#FFFFCC">文件屬性</td>
<td align="center" bgcolor="#FFFFCC">文件大小</td>
<td align="center" bgcolor="#FFFFCC">操作</td>
</tr>';
foreach($s as $value)
{
echo '
<tr>
<td height="25" bgcolor="#FFFFFF">'.folder($path,$value).'</td>
<td align="center" bgcolor="#FFFFFF">'.filetype($path.$value).'</td>
<td align="right" bgcolor="#FFFFFF">'.round(filesize($path.$value)/1024).'kb</td>
<td align="center" bgcolor="#FFFFFF"><a href="?action=edit&path='.$path.'\\'.$value.'&type='.filetype($path.$value).'">編輯</a> | <a href="?action=del&path='.$path.'\\'.$value.'&type='.filetype($path.$value).'" onClick="return confirm(\'確定刪除->'.$value.'\');">刪除</a></td>
</tr>';
}
echo "</table>";
break;
}
?>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved