This article mainly introduces php file operations and related classes. The example analyzes php operation skills for file and directory creation, deletion, replication, and check. For more information, see
This article mainly introduces php file operations and related classes. The example analyzes php operation skills for file and directory creation, deletion, replication, and check. For more information, see
This example describes the PHP file operation classes. Share it with you for your reference. The details are as follows:
<? Php class file_dir {function check_exist ($ filename) // check whether the directory or file exists {if (file_exists ($ filename) {return true;} else return false ;} function create_dir ($ dirname, $ mode = 0777) // only the first-level directory can be created at a time. {if (is_null ($ dirname) | $ dirname = "") return false; if (! Is_dir ($ dirname) {return mkdir ($ dirname, $ mode) ;}} function createDir ($ aimUrl) // you can create a multilevel directory {$ aimUrl = str_replace ('\\',' http://www.jb51.net/ ', $ AimUrl); $ aimDir = ''; $ arr = explode (' http://www.jb51.net/ ', $ AimUrl); foreach ($ arr as $ str) {$ aimDir. = $ str .' http://www.jb51.net/ '; If (! File_exists ($ aimDir) {mkdir ($ aimDir) ;}} function delete_dir ($ dirname) // Delete the directory {if ($ this-> 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." http://www.jb51.net/ ". $ 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 {I F (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 ;}} function 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);?>
I hope this article will help you with php programming.
,