Introduction: This is a detailed page of PHP file operations. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 335093 'rolling = 'no'>
<? /* ** File type ** example: * 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: createfile (' B/1/2/3.exe ') under the B/1/2/folder '); test the creation of a 3.exe file * fileutil: copydir ('B', 'd/E') under B/1/2/folder. Test the creation of a D/E folder in the copy folder, 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 the mobile folder to create a B/ C folder, move the content in folder A, and delete folder * fileutil: movefile ('B/1/2/3.exe', 'B/D/3.exe '); create a B/D folder and move 3.exe in B/1/2 to * fileutil: unlinkfile ('B/D/3.exe '); test the deletion of 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); Foreach ($ arr as $ Str) {$ aimdir. = $ Str. '/'; If (! File_exists ($ aimdir) {mkdir ($ aimdir );}}} /*** 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 ($ aimurl); fileutil: createdir ($ AI Mdir); 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_repla Ce ('', '/', $ 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 $ aimdir *@ 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 ;}}?> <? Phpclass fileutil {// create a folder function createdir ($ aimurl) {$ aimurl = str_replace ('', '/', $ aimurl); $ aimdir = ''; $ arr = explode ('/', $ aimurl); foreach ($ arr as $ Str) {$ aimdir. = $ Str. '/'; If (! File_exists ($ aimdir) {mkdir ($ aimdir) ;}}// create a file function createfile ($ aimurl, $ overwrite = false) {If (file_exists ($ aimurl) & $ overwrite = false) {return false;} elseif (file_exists ($ aimurl) & $ overwrite = true) {fileutil: unlinkfile ($ aimurl );} $ aimdir = dirname ($ aimurl); fileutil: createdir ($ aimdir); touch ($ aimurl); Return true;} // function of moving the folder 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 fileutil: unlinkdir ($ olddir);} // move the file 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 the folder function unlinkdir ($ aimdir) {$ aimdir = str_replace ('', '/', $ 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 the file function unlinkfile ($ aimurl) {If (file_exists ($ aimurl )) {unlink ($ aimurl); Return true;} else {return false ;}// copy the function copydir ($ olddir, $ aimdir, $ overwrite = false) folder) {$ 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 the file 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 ;}}?>
More articles on "php file operations"
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/335093.html pageno: 10.