PHP file and folder operations (create, delete, move, copy ),
Create a FileUtil. php file. The content and calling method are as follows:
<? Example of php/*** file control **: * FileUtil: createDir ('A/1/2/3'); Create a folder named a/1/2/3 * FileUtil:: createFile ('B/1/2/3'); Create a 3 File * FileUtil under the B/1/2/folder to test the file creation :: createFile ('B/1/2/3.exe'); Create a 3.exe file * FileUtil: copyDir ('B', 'd/E') under file B/1/2/folder '); create a d/e folder in the copy folder and copy the content in the B folder to * FileUtil: copyFile ('B/1/2/3.exe ', 'B/B/3.exe'); Create a B/B folder and copy the 3.exe file in the B/1/2 folder to * FileUtil: moveDir ('A /', 'B/C'); test migration Create a folder B/c, move the content in folder a, and delete the folder * FileUtil: moveFile ('B/1/2/3.exe ', 'B/d/3.exe'); test the mobile file to create a B/d folder, and move 3.exe in B/1/2 to * FileUtil :: unlinkFile ('B/d/3.exe'); test the function of deleting the B/d/3.exe file * FileUtil: unlinkDir ('D '); test deleting a folder and deleting a d folder */class FileUtil {/*** create a folder ** @ param string $ aimUrl * @ return viod */function createDir ($ aimUrl) {$ aimUrl = str_replace ('', '/', $ aimUrl); $ aimDir =''; $ arr = explode ('/', $ AimUrl); $ result = true; foreach ($ arr as $ str) {$ aimDir. = $ str. '/'; if (! File_exists ($ aimDir) {$ result = mkdir ($ aimDir);} return $ result ;} /*** create a file ** @ param string $ aimUrl * @ param boolean $ overWrite this parameter controls whether to overWrite the original file * @ return boolean */function createFile ($ aimUrl, $ overWrite = false) {if (file_exists ($ aimUrl) & $ overWrite = false) {return false;} elseif (file_exists ($ aimUrl) & $ overWrite = true) {FileUtil: unlinkFile ($ aimUrl);} $ aimDir = dirname ($ aimU Rl); FileUtil: createDir ($ aimDir); touch ($ aimUrl); return true ;} /*** move folder ** @ param string $ oldDir * @ param string $ aimDir * @ param boolean $ overWrite this parameter controls whether to overWrite the original file * @ return boolean */function moveDir ($ oldDir, $ aimDir, $ overWrite = false) {$ aimDir = str_replace ('', '/', $ aimDir); $ aimDir = substr ($ aimDir,-1) = '/'? $ AimDir: $ aimDir. '/'; $ oldDir = str_replace (','/', $ oldDir); $ oldDir = substr ($ oldDir,-1) = '/'? $ OldDir: $ oldDir. '/'; if (! Is_dir ($ oldDir) {return false;} if (! File_exists ($ aimDir) {FileUtil: createDir ($ aimDir) ;}@$ dirHandle = opendir ($ oldDir); if (! $ DirHandle) {return false;} while (false! ==( $ File = readdir ($ dirHandle) {if ($ file = '. '| $ file = '.. ') {continue;} if (! Is_dir ($ oldDir. $ file) {FileUtil: moveFile ($ oldDir. $ file, $ aimDir. $ file, $ overWrite);} else {FileUtil: moveDir ($ oldDir. $ file, $ aimDir. $ file, $ overWrite) ;}} closedir ($ dirHandle); return rmdir ($ oldDir );} /*** move file ** @ param string $ fileUrl * @ param string $ aimUrl * @ param boolean $ overWrite this parameter controls whether to overWrite the original file * @ return boolean */function moveFile ($ fileUrl, $ aimUrl, $ overWrite = false) {If (! File_exists ($ fileUrl) {return false;} if (file_exists ($ aimUrl) & $ overWrite = false) {return false;} elseif (file_exists ($ aimUrl) & $ overWrite = true) {FileUtil: unlinkFile ($ aimUrl);} $ aimDir = dirname ($ aimUrl); FileUtil: createDir ($ aimDir ); rename ($ fileUrl, $ aimUrl); return true;}/*** Delete folder ** @ param string $ aimDir * @ return boolean */function unlinkDir ($ aimDir) {$ aimDir = str_r Eplace ('', '/', $ aimDir); $ aimDir = substr ($ aimDir,-1) = '/'? $ AimDir: $ aimDir. '/'; if (! Is_dir ($ aimDir) {return false;} $ dirHandle = opendir ($ aimDir); while (false! ==( $ File = readdir ($ dirHandle) {if ($ file = '. '| $ file = '.. ') {continue;} if (! Is_dir ($ aimDir. $ file) {FileUtil: unlinkFile ($ aimDir. $ file);} else {FileUtil: unlinkDir ($ aimDir. $ file) ;}} closedir ($ dirHandle); return rmdir ($ aimDir );} /*** delete file ** @ param string $ aimUrl * @ return boolean */function unlinkFile ($ aimUrl) {if (file_exists ($ aimUrl )) {unlink ($ aimUrl); return true;} else {return false;}/*** copy folder ** @ param string $ oldDir * @ param string $ aimD Ir * @ param boolean $ overWrite this parameter controls whether to overWrite the original file * @ return boolean */function copyDir ($ oldDir, $ aimDir, $ overWrite = false) {$ aimDir = str_replace ('', '/', $ aimDir); $ aimDir = substr ($ aimDir,-1) = '/'? $ AimDir: $ aimDir. '/'; $ oldDir = str_replace (','/', $ oldDir); $ oldDir = substr ($ oldDir,-1) = '/'? $ OldDir: $ oldDir. '/'; if (! Is_dir ($ oldDir) {return false;} if (! File_exists ($ aimDir) {FileUtil: createDir ($ aimDir) ;}$ dirHandle = opendir ($ oldDir); while (false! ==( $ File = readdir ($ dirHandle) {if ($ file = '. '| $ file = '.. ') {continue;} if (! Is_dir ($ oldDir. $ file) {FileUtil: copyFile ($ oldDir. $ file, $ aimDir. $ file, $ overWrite);} else {FileUtil: copyDir ($ oldDir. $ file, $ aimDir. $ file, $ overWrite) ;}return closedir ($ dirHandle );} /*** copy a file ** @ param string $ fileUrl * @ param string $ aimUrl * @ param boolean $ overWrite this parameter controls whether to overWrite the original file * @ return boolean */function copyFile ($ fileUrl, $ aimUrl, $ overWrite = false) {if (! File_exists ($ fileUrl) {return false;} if (file_exists ($ aimUrl) & $ overWrite = false) {return false;} elseif (file_exists ($ aimUrl) & $ overWrite = true) {FileUtil: unlinkFile ($ aimUrl);} $ aimDir = dirname ($ aimUrl); FileUtil: createDir ($ aimDir ); copy ($ fileUrl, $ aimUrl); return true ;}}?>
Another call method:
$fu = new FileUtil();$fu->copyFile('a/1/2/3', 'a/1/2/4');
We recommend a 360-disk search engine developed in my spare time (www.360panso.com)