PHP implementation of the Traversal folder under all the files, edit delete _php tips

Source: Internet
Author: User
Copy Code code as follows:

<?php
/*
Traverse folder; Functions: (a) can delete the file (b) editable text, Web page file (c) can be deleted folder, if the folder is empty (d) can be established files, folders, modify folder name;
lost63.com original, reprint please indicate the source. AC qq:35501547
*/
$path = $DOCUMENT _root;
$path =str_replace ("/", "\ \", $path);
Specify folder
$path = $path. " \\";
if ($_get[' folder '])
{
$path. =$_get[' folder ']. " \\";
}
This page path, the following will be used when passed back;
$url =$_server[' php_self '];
If it is a folder, the 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 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>
&LT;TD height= bgcolor= "#99CC00" >>><strong> Edit file </strong> > <a href= "'. $url. '" > Return </a></td>
</tr>
<tr>
&LT;TD height= "O" align= "left" bgcolor= "#FFFFCC" >-> filename: '. $_get[' path '. ' </td>
</tr>
<tr>
&LT;TD align= "center" bgcolor= "#FFFFFF" ><textarea name= "textarea" cols= "135" rows= ">". $result. ' </textarea></td>
</tr>
<tr>
&LT;TD align= "center" bgcolor= "#FFFFFF" ><input type= "Submit" name= "Submission" value= "submitted" >
<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>
&LT;TD height= bgcolor= "#99CC00" >>><strong> new file </strong> > <a href= "'. $url. '" > Return </a></td>
</tr>
<tr>
&LT;TD height= "align=" "left" bgcolor= "#FFFFCC" >-> folder renamed:
<input name= "filename" type= "text" value= "'. $_get[' path '." "Size=" ></td>
</tr>
<tr>
&LT;TD align= "center" bgcolor= "#FFFFFF" ><input type= "Submit" name= "Submission" value= "submitted" >
<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>
&LT;TD height= bgcolor= "#99CC00" >>><strong> new file </strong> > <a href= "'. $url. '" > Return </a></td>
</tr>
<tr>
&LT;TD height= "align=" "left" bgcolor= "#FFFFCC" >-> filename:
<input name= "filename" type= "text" value= "'. $_get[' path '." "Size=" ></td>
</tr>
<tr>
&LT;TD align= "center" bgcolor= "#FFFFFF" ><textarea name= "textarea" cols= "135" rows= "M" > Input
</textarea></td>
</tr>
<tr>
&LT;TD align= "center" bgcolor= "#FFFFFF" ><input type= "Submit" name= "Submission" value= "submitted" >
<input type= "reset" name= "Submit2" value= "reset" ></td>
</tr>
</table>
</form> ';
Break
Case "FileSave";
Include points to create a file, otherwise create a folder
if (Strpos ($_post[' filename '], "."))
{
$file =fopen ($_post[' filename '], "w");
Fwrite ($file, $_post[' textarea ']);
Fclose ($file);
}else{
If the folder exists, it exits and does not exist!
if (file_exists ($_post[' filename '))
{
Exit
}else{
mkdir ($_post[' filename ')];
}
}
echo "<script type=\" text/javascript\ ">alert (' Congratulations," $_post[' filename '). " Build success! '); Location.href=\ "". $url. " \ ";</script>";
Break
Case "Dir_rename";
Rename ($_get[' path '],$_post[' filename ')];
echo "<script type=\" text/javascript\ ">alert (' Congratulations," $_post[' filename '). " Renamed 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>
&LT;TD height= colspan= "4" bgcolor= "#99CC00" >>><strong> file management </strong> > <a href= "? Action=addfile&path= '. $path. ' " > Establish documents </a> > <a href= "'. $url. '" > Return </a></td>
</tr>
<tr>
&LT;TD height= align= "center" bgcolor= "#FFFFCC" > Files/Folders </td>
&LT;TD align= "center" bgcolor= "#FFFFCC" > File Properties </td>
&LT;TD align= "center" bgcolor= "#FFFFCC" > File size </td>
&LT;TD align= "center" bgcolor= "#FFFFCC" > Operations </td>
</tr> ';
foreach ($s as $value)
{
Echo '
<tr>
&LT;TD height= "bgcolor=" #FFFFFF ">". Folder ($path, $value). ' </td>
&LT;TD align= "center" bgcolor= "#FFFFFF >". FileType ($path. $value). ' </td>
&LT;TD align= "Right" bgcolor= "#FFFFFF" > "Round (FileSize ($path. $value)/1024). ' Kb</td>
&LT;TD align= "center" bgcolor= "#FFFFFF" ><a href= "action=edit&path=". $path. ' \ $value. ' &type= '. FileType ($path. $value). ' " > Edit </a> | <a href= "? action=del&path= '. $path. ' \ $value. ' &type= '. FileType ($path. $value). ' Onclick= ' return confirm (\ ' OK delete-> '. $value. ') \');" > Delete </a></td>
</tr> ';
}
echo "</table>";
Break
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.