PHP implements traversal of all files in the folder, editing and deleting

Source: Internet
Author: User

CopyCode The Code is as follows: <? PHP
/*
Function: (a) delete a file (B) edit a file, and delete a folder on a webpage (C), provided that the folder is empty (d) create a file or folder, modify the folder name;
Lost63.com original, reprinted please indicate the source. Exchange QQ: 35501547
*/
$ Path = $ document_root;
$ Path = str_replace ("/", "\", $ PATH );
// Specify a folder
$ Path = $ path ."\\";
If ($ _ Get ['folder'])
{
$ Path. = $ _ Get ['folder']. "\";
}
// The path of this page, which will be used when the following is returned;
$ Url = $ _ server ['php _ Self '];
// If it is a folder, a link is 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, deletion successful! '); 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 file </strong> <a href = "'. $ URL. '"> return </a> </TD>
</Tr>
<Tr>
<TD Height = "25" align = "Left" bgcolor = "# ffffcc">-> 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> New File </strong> <a href = "'. $ URL. '"> return </a> </TD>
</Tr>
<Tr>
<TD Height = "25" align = "Left" bgcolor = "# ffffcc">-> folder rename:
<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 successful! '); 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> New File </strong> <a href = "'. $ URL. '"> return </a> </TD>
</Tr>
<Tr>
<TD Height = "25" align = "Left" bgcolor = "# ffffcc">-> 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"> input content
</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 ";
// Create a file if it contains a vertex. Otherwise, create a folder.
If (strpos ($ _ post ['filename'], ".")
{
$ File = fopen ($ _ post ['filename'], "W ");
Fwrite ($ file, $ _ post ['textarea ']);
Fclose ($ file );
} Else {
// Exit if the folder exists. If the folder does not exist, it is created!
If (file_exists ($ _ post ['filename'])
{
Exit;
} Else {
Mkdir ($ _ post ['filename']);
}
}
Echo "<SCRIPT type = \" text/JavaScript \ "> alert ('Congratulations,". $ _ post ['filename']. "created successfully! '); Location. href = \ "". $ URL. "\"; </SCRIPT> ";
Break;
Case "dir_rename ";
Rename ($ _ Get ['path'], $ _ post ['filename']);
Echo "<SCRIPT type = \" text/JavaScript \ "> alert ('Congratulations,". $ _ post ['filename']. "renamed! '); 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 = "# cccccccc">
<Tr>
<TD Height = "25" colspan = "4" bgcolor = "#99cc00" >>> <strong> file management </strong> <a href = "? Action = AddFile & Path = '. $ path. '"> create a file </a> <a href = "'. $ URL. '"> return </a> </TD>
</Tr>
<Tr>
<TD Height = "25" align = "center" bgcolor = "# ffffcc"> files/folders </TD>
<TD align = "center" bgcolor = "# ffffcc"> file attributes </TD>
<TD align = "center" bgcolor = "# ffffcc"> 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 ). '"> edit </a> | <a href = "? Action = del & Path = '. $ path. '\\'. $ value. '& type = '. filetype ($ path. $ value ). '"onclick =" Return confirm (\ 'Confirm to 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.