PHP file operations related class instance, _ PHP Tutorial

Source: Internet
Author: User
Php file operations related class instances ,. Php file operation-related classes. this example describes php file operation-related classes. Share it with you for your reference. Details: phpclassfile_dir {functioncheck_exist ($ filena php file operation-related class instance,

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 ('\', '/', $ aimUrl); $ aimDir = ''; $ arr = explode ('/', $ aimUrl); foreach ($ arr as $ str) {$ aimDir. = $ str. '/'; 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. "/". $ 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);?>

I hope this article will help you with php programming.

Examples of PHP file operations. Share it with you for your reference. Php class file_dir {function check_exist ($ filena...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.