PHP implementation of the traversal folder under all files edit delete

  • 2020-03-31 20:16:38
  • OfStack


<?php 
 
$path=$DOCUMENT_ROOT; 
$path=str_replace("/","\",$path); 
//Specified folder
$path=$path."\"; 
if($_GET['folder']) 
{ 
$path.=$_GET['folder']."\"; 
} 
//This page path will be used when the following returns;
$url=$_SERVER['PHP_SELF']; 
//If it is a folder, a link will be added.
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(' congratulations , Delete the success !');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>  Edit the file </strong> > <a href="'.$url.'"> return </a></td> 
</tr> 
<tr> 
<td height="25" align="left" bgcolor="#FFFFCC">-> The file name :'.$_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=" submit "> 
<input type="reset" name="Submit2" value=" reset "></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>  The new file </strong> > <a href="'.$url.'"> return </a></td> 
</tr> 
<tr> 
<td height="25" align="left" bgcolor="#FFFFCC">-> Folder name : 
<input name="filename" type="text" value="'.$_GET['path'].'" size="50"></td> 
</tr> 
<tr> 
<td align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value=" submit "> 
<input type="reset" name="Submit2" value=" reset "></td> 
</tr> 
</table> 
</form>'; 
} 
break; 
case "editsave"; 
$file=fopen($_GET['path'],"w"); 
fwrite($file,$_POST['textarea']); 
fclose($file); 
echo "<script type="text/javascript">alert(' congratulations , Edit success !');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>  The new file </strong> > <a href="'.$url.'"> return </a></td> 
</tr> 
<tr> 
<td height="25" align="left" bgcolor="#FFFFCC">-> The file name : 
<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"> The input  
</textarea></td> 
</tr> 
<tr> 
<td align="center" bgcolor="#FFFFFF"><input type="submit" name="Submit" value=" submit "> 
<input type="reset" name="Submit2" value=" reset "></td> 
</tr> 
</table> 
</form>'; 
break; 
case "filesave"; 
//Include points create files, otherwise create folders
if(strpos($_POST['filename'],".")) 
{ 
$file=fopen($_POST['filename'],"w"); 
fwrite($file,$_POST['textarea']); 
fclose($file); 
}else{ 
//If the folder exists, exit; if not, create!
if(file_exists($_POST['filename'])) 
{ 
exit; 
}else{ 
mkdir($_POST['filename']); 
} 
} 
echo "<script type="text/javascript">alert(' congratulations ,".$_POST['filename']." Establish a successful !');location.href="".$url."";</script>"; 
break; 
case "dir_rename"; 
rename($_GET['path'],$_POST['filename']); 
echo "<script type="text/javascript">alert(' congratulations ,".$_POST['filename']." Incognito success !');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>  File management </strong> > <a href="?action=addfile&path='.$path.'"> Set up files </a> > <a href="'.$url.'"> return </a></td> 
</tr> 
<tr> 
<td height="25" align="center" bgcolor="#FFFFCC"> file / folder </td> 
<td align="center" bgcolor="#FFFFCC"> File attributes </td> 
<td align="center" bgcolor="#FFFFCC"> The file size </td> 
<td align="center" bgcolor="#FFFFCC"> operation </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).'"> The editor </a> | <a href="?action=del&path='.$path.'\'.$value.'&type='.filetype($path.$value).'" onClick="return confirm(' Sure to delete ->'.$value.'');"> delete </a></td> 
</tr>'; 
} 
echo "</table>"; 
break; 
} 
?>

Related articles: