This article mainly introduces php file operation related classes. The example analyzes php operation skills for file and directory creation, deletion, replication, and check, for more information about php file operations, see the next article. Examples of php file and directory creation, deletion, replication, and check operations are analyzed, for more information, see
This example describes the php file operation classes. Share it with you for your reference. The details are as follows:
Check_exist ($ dirname) and is_dir ($ dirname) {if (! $ Dirhandle = opendir ($ dirname) return false; while ($ file = readdir ($ dirhandle ))! = False) {if ($ file = ". "or $ file = ".. ") continue; $ file = $ dirname. DIRECTORY_SEPARATOR. $ file; // indicates that $ file is the $ dir subdirectory if (is_dir ($ file) {$ this-> delete_dir ($ file );} else {unlink ($ file) ;}} closedir ($ dirhandle); return rmdir ($ dirname);} else return false;} function copy_dir ($ dirfrom, $ dirto) // Copy the directory {if (! Is_dir ($ dirfrom) return false; if (! Is_dir ($ dirto) mkdir ($ dirto); $ dirhandle = opendir ($ dirfrom); if ($ dirhandle) {while (false! ==( $ File = readdir ($ dirhandle) {if ($ file = ". "or $ file = ".. ") continue; $ filefrom = $ dirfrom. DIRECTORY_SEPARATOR. $ file; // indicates that $ file is a $ dir subdirectory $ fileto = $ dirto. DIRECTORY_SEPARATOR. $ file; if (is_dir ($ filefrom) {$ this-> copy_dir ($ filefrom, $ fileto);} else {if (! File_exists ($ fileto) copy ($ filefrom, $ fileto) ;}} closedir ($ dirhandle);} function getdir_size ($ dirname) // Obtain the directory size {if (! File_exists ($ dirname) or! Is_dir ($ dirname) return false; if (! $ Handle = opendir ($ dirname) return false; $ size = 0; while (false! ==( $ File = readdir ($ handle) {if ($ file = ". "or $ file = ".. ") continue; $ file = $ dirname. "/". $ file; if (is_dir ($ file) {$ size + = $ this-> getdir_size ($ file );} else {$ size + = filesize ($ file) ;}} closedir ($ handle); return $ size;} function getReal_size ($ size) // unit automatic conversion function {$ kb = 1024; $ mb = $ kb * 1024; $ gb = $ mb * 1024; $ tb = $ gb * 1024; if ($ size <$ kb) return $ size. "B"; if ($ size >=$ kb and $ size <$ mb) return round ($ size/$ kb, 2 ). "KB"; if ($ size >=$ mb and $ size <$ gb) return round ($ size/$ mb, 2 ). "MB"; if ($ size >=$ gb and $ size <$ tb) return round ($ size/$ gb, 2 ). "GB"; if ($ size >=$ tb) return round ($ size/$ tb, 2 ). "TB";} function copy_file ($ srcfile, $ dstfile) {if (is_file ($ srcfile) {if (! File_exists ($ dstfile) return copy ($ srcfile, $ dstfile);} else return false;} function unlink_file ($ filename) // delete the file {if ($ this-> check_exist ($ filename) and is_file ($ filename) {return unlink ($ filename);} else return false ;} function getsuffix ($ filename) // Obtain the filename suffix {if (file_exists ($ filename) and is_file ($ filename) {return end (explode (". ", $ filename) ;}} function input_content ($ filename, $ str )/ /Write the string to the file {if (function_exists (file_put_contents) {file_put_contents ($ filename, $ str);} else {$ fp = fopen ($ filename, "wb "); fwrite ($ fp, $ str); fclose ($ fp) ;}} function output_content ($ filename) // read the entire file content to a string {if (function_exists (file_get_contents) {return file_get_contents ($ filename);} else {$ fp = fopen ($ filename, "rb"); $ str = fread ($ fp, filesize ($ filename); fclose ($ fp); return $ str ;}} func Tion output_to_array ($ filename) // read the file content to an array {$ file = file ($ filename); $ arr = array (); foreach ($ file as $ value) {$ arr [] = trim ($ value);} return $ arr ;}// $ dir = new file_dir; // $ size = $ dir-> getdir_size ("F:/wamp/www/class/images"); // echo $ dir-> getReal_size ($ size);?>