Traverse all files and folders in a folder and copy the matched files to the same directory. In the following example, all the html files in the "/www/pooy/Ke" directory are copied to the "/www/pooy/bk" directory.
The code is as follows: |
Copy code |
$ Dir = "/www/pooy/shortke "; Static $ dir_list = 0; Static $ file_list = 0; Function listfile ($ dir ){ Global $ dir_list, $ file_list; $ D = dir ($ dir ); While ($ entry = $ d-> read ()){ $ Tem_curnt = $ dir. "/". $ entry; // Echo $ tem_curnt. "<br> "; If ($ entry = "." | $ entry = "...") continue; If (is_dir ($ tem_curnt )){ Listfile ($ tem_curnt ); Echo "folder". $ tem_curnt. "<br> "; $ Dir_list ++; } Elseif (is_file ($ tem_curnt )) { Echo "file". $ tem_curnt. "<BR> "; _ Copy ($ tem_curnt, "/www/pooy/bk "); $ File_list ++; } } $ D-> close (); } Function _ copy ($ src, $ dst ){ If (! Is_dir ($ src )){ If (! Copy ($ src, $ dst )){ Return _ log ('unable to copy files', $ src ); } } Else { Mkdir ($ dst ); $ Ls = scandir ($ src ); For ($ I = 0; $ I <count ($ ls); $ I ++ ){ If ($ ls [$ I] = '.' OR $ ls [$ I] = '..') continue; $ _ Src = $ src. '/'. $ ls [$ I]; $ _ Dst = $ dst. '/'. $ ls [$ I]; If (is_dir ($ _ src )){ If (! _ Copy ($ _ src, $ _ dst )){ Return _ log ('unable to copy files', $ _ src ); } } Else { If (! Copy ($ _ src, $ _ dst )){ Return _ log ('unable to copy files', $ _ src ); } } } } Return TRUE; } Listfile ($ dir ); Echo "directory count:". $ dir_list; Echo "<br> "; Echo "file count:". $ file_list;
|