PHP visitfile () traversal of the specified folder Function

Source: Internet
Author: User

Note: visitfile () has a small number of modifications CopyCode The Code is as follows: <?
// View the objects in the specified folder
$ Filelist = array ();
Function visitfile ($ PATH)
{
Global $ filelist;
$ Path = str_replace ("\", "/", $ PATH );
$ Fdir = Dir ($ PATH );
While ($ file = $ fdir-> Read ())! = False)
{
If ($ file = '.' | $ file = '..') {continue ;}
$ Pathsub = preg_replace ("*/{2,} *", "/", $ path. "/". $ file); // replace multiple backslash
$ Filelist [] = is_dir ($ pathsub )? $ Pathsub. "/": $ pathsub;
If (is_dir ($ pathsub) {visitfile ($ pathsub );}
}
$ Fdir-> close ();
Return $ filelist;
}
?>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Form method = "get">
<?
$ Path = str_replace ("\", "/", $ PATH );
$ Path = preg_replace ("*/{2,} *", "/", $ PATH );
?>
Path: <input type = "text" name = "path" id = "path" value = "<? = $ Path;?> "/> <Br>
<Li> disk root directory/</LI>
<Li> Local Network./phpMyAdmin </LI>
<Li> Local disk file: // C: or C: </LI>
<Br>
<Input name = "action" type = "Submit" id = "action" value = "View"/>
<Input name = "action" type = "Submit" id = "action" value = "delete" onclick = "If (! Confirm ('delete all subfolders and subfolders of '+ path. Value +? ') Return false; "/>
</Form>
<?
If (! Empty ($ PATH )){
$ Path = preg_replace ("*/{2,} *", "/", $ PATH );
$ Files = visitfile ($ PATH );
Switch (strtolower ($ _ Get ["action"])
{
Case "View ":
Foreach ($ files as $ key => $ value)
{
Printf ("No. % 4D · % S <br> \ r \ n", $ key + 1, $ value );
}
Break;
Case "delete ":
$ Failefiles = array ();
Foreach (array_reverse ($ files) as $ value)
{
If (! Unlink ($ value ))
{
Array_push ($ failefiles, $ value );
}
}
If (! Unlink ($ PATH) {array_push ($ failefiles, $ PATH );}
If (count ($ failefiles)> 0)
{
Printf ("<br> failed file deletion (% d): <p> \ r \ n", count ($ failefiles ));
Foreach ($ failefiles as $ key => $ value)
{
Printf ("No. % 4D · % S <br> \ r \ n", $ key + 1, $ value );
}
}
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.