| A certain University provides a php file operation class with a strong scholarly atmosphere. if you are a friend who just stepped out of the school, may you please refer to the code in this article to see if you have ever known each other? The programmer's house collects data for your reference.
The code is as follows:
FileName_str = $ fileName_str; $ this-> fileOpenMethod_str = $ fileOpenMethod_str;} function _ destruct () {// destructor} public function _ get ($ valName_val) // name of the data member to be retrieved {// special function, get the value of the specified name data Member return $ this-> $ valName_val ;} private function on_error ($ errMsg_str = 'unkown Error! ', $ ErrNo_int = 0) // error message, error code {echo 'Program error :'. $ errMsg_str. 'error code :'. $ errNo_int; // error handling function} public function open () {// open the corresponding file and return the file resource ID. // select the open mode switch according to fileOpenMethod_str ($ this-> fileOpenMethod_str) {case 'readonly': $ openMethod_str = 'R'; // read-only, pointer pointing to the file header break; case 'readwrite': $ openMethod_str = 'R + '; // read/write, the pointer points to the file header break; case 'writeandinit ': $ openMethod_str = 'w'; // write-only. if the pointer points to the file header, the file size is truncated to zero. if the pointer does not exist, the break is created. case 'readwritea NdInit ': $ openMethod_str = 'R +'; // Read/Write. when the pointer points to the file header and the size is cut to zero, the break is created if the file does not exist. case 'writeandadd': $ openMethod_str = 'a '; // write only. the pointer points to the end of the file. if the pointer does not exist, the break is created. case 'readwriteandadd': $ openMethod_str = 'A + '; // Read/Write. the pointer points to the end of the file, if it does not exist, create a break. default: $ this-> on_error ('open method error! ', 310); // error handling exit;} // open the file if (! $ Fp_res = fopen ($ this-> fileName_str, $ openMethod_str) {$ this-> on_error ('Can \'t open the file! ', 301); // error handling exit;} return $ fp_res;} public function close ($ fp_res) // resource ID returned by open {// Close the opened file if (! Fclose ($ fp_res) {$ this-> on_error ('Can \'t close the file! ', 302); // error handling exit;} public function write () // $ fp_res, $ data_str, $ length_int: file resource ID, written string, length control {// write the string string_str to the file fp_res, you can control the write length length_int // determine the number of parameters, call the relevant function $ argNum_int = func_num_args (); // number of parameters $ fp_res = func_get_arg (0); // file resource ID $ data_str = func_get_arg (1); // The written string if ($ argNum_int = 3) {$ length_int = func_get_arg (2); // if (! Fwrite ($ fp_res, $ data_str, $ length_int) {$ this-> on_error ('Can \'t write the file! ', 303); // error handling exit;} else {if (! Fwrite ($ fp_res, $ data_str) {$ this-> on_error ('Can \'t write the file! ', 303); // error handling exit; }}} public function read_line () // $ fp_res, $ length_int: file resource ID, read length {// read a line of string from the file fp_res to control the length // determine the number of parameters $ argNum_int = func_num_args (); $ fp_res = func_get_arg (0 ); if ($ argNum_int = 2) {$ length_int = func_get_arg (1); if ($ string_str =! Fgets ($ fp_res, $ length_int) {$ this-> on_error ('Can \'t read the file! ', 304); // error handling exit;} return $ string_str;} else {if (! $ String_str = fgets ($ fp_res) {$ this-> on_error ('Can \'t read the file! ', 304); // error handling exit;} return $ string_str;} public function read ($ fp_res, $ length_int) // file resource ID, length control {// read the file fp_res. The maximum length is length_int if (! $ String_str = fread ($ fp_res, $ length_int) {$ this-> on_error ('Can \'t read the file! ', 305); // error handling exit;} return $ string_str;} public function is_exists ($ fileName_str) // file name {// check whether the file $ fileName_str exists, if yes, true is returned. If no, false is returned. return file_exists ($ fileName_str );} /****************** obtain the file size ****************** * ** // * get the file fileName_str size $ fileName_str is the file path and name. The Returned file size value */public function get_file_size ($ fileName_str) // file name {return filesize ($ fileName_str );} *********** * ******** // * $ FileSize_int file size, in bytes. The size of the file with measurement units after conversion is returned */public function change_size_express ($ fileSize_int) // file name {if ($ fileSize_int> 1024) {$ fileSizeNew_int = $ fileSize_int/1024; // Convert to K $ unit_str = 'KB'; if ($ fileSizeNew_int> 1024) {$ fileSizeNew_int = $ fileSizeNew_int/1024; // Convert to M $ unit_str = 'mb';} $ fileSizeNew_arr = explode ('. ', $ fileSizeNew_int); $ fileSizeNew_str = $ fileSizeNew_arr [0]. '. '. substr ($ fileSizeNew_ar R [1], 0, 2 ). $ unit_str;} return $ fileSizeNew_str ;} /****************** rename a file ******************* ** // * rename the file specified by oldname_str to newname_str $ oldName_str is the original name of the file $ newName_str is the new name of the file, and an error message is returned */public function rename_file ($ oldName_str, $ newName_str) {if (! Rename ($ oldName_str, $ newName_str) {$ this-> on_error ('Can \'t rename file! ', 308); exit ;}} ******************* ** // * delete the file specified by filename_str $ fileName_str the path and name of the file to be deleted. an error message is returned */public function delete_file ($ fileName_str) // {if (! Unlink ($ fileName_str) {$ this-> on_error ('Can \'t delete file! ', 309); // error handling exit ;}} ***************** * *** // * get the extension of the file specified by filename_str $ fileName_str to retrieve the file path and name of the type. return the file extension */public function get_file_type ($ fileName_str) {$ fileNamePart_arr = explode ('. ', $ fileName_str); while (list (, $ fileType_str) = each ($ fileNamePart_arr) {$ type_str = $ fileType_str;} return $ type_str ;} /****************** determine whether the file is of a specified file type ************** * ****** // * $ fileType_str specifies the file type $ FileName_str return false or true */public function is_the_type ($ fileName_str, $ fileType_arr) {$ cheakFileType_str = $ this-> get_file_type ($ fileName_str ); if (! In_array ($ cheakFileType_str, $ fileType_arr) {return false;} else {return true ;}} /******************* Upload an object, and return the uploaded file information ********************** // $ fileName_str local file name $ filePath path, if $ filePath is str, upload the file to the same directory and name it with-, 3 .., if arr is used, it is named in sequence $ allowType_arr to allow the type of files to be uploaded. leave it blank without limiting the maximum value allowed by $ maxSize_int. leave it blank and do not limit the two-dimensional array of new file information returned: $ reFileInfo_arr */public function upload_file ($ fileName_str, $ filePath, $ allowType_arr = '', $ maxSi Ze_int = '') {$ fileName_arr = $ _ FILES [$ fileName_str] ['name']; // file name $ fileTempName_arr =$ _ FILES [$ fileName_str] ['tmp _ name']; // cached file $ fileSize_arr = $ _ FILES [$ fileName_str] ['size']; // get the file size $ reFileInfo_arr = array (); $ num = count ($ fileName_arr)-1; for ($ I = 0; $ I <= $ num; $ I ++) {if ($ fileName_arr [$ I]! = '') {If ($ allowType_arr! = ''And! $ This-> is_the_type ($ fileName_arr [$ I], $ allowType_arr) // checks whether The file type is allowed. {$ this-> on_error ('The file is not allowed type! ', 310); // break for error handling;} if ($ maxSize_int! = ''And $ fileSize_arr [$ I]> $ maxSize_int) {$ this-> on_error ('The file is too big! ', 311); // break for error handling;} $ j = $ I + 1; $ fileType_str = $ this-> get_file_type ($ fileName_arr [$ I]); // Obtain the object type if (! Is_array ($ filePath) {$ fileNewName_str = $ filePath. '-'. ($ j ). '. '. $ fileType_str;} else {$ fileNewName_str = $ filePath_arr [$ I]. '. '. $ fileType_str;} copy ($ fileTempName_arr [$ I], $ fileNewName_str); // upload the file unlink ($ fileTempName_arr [$ I]); // delete the cached file // --------------- save the file information. // $ doFile_arr = explode ('/', $ fileNewName_str); $ doFile_num_int = count ($ doFile_arr)-1; $ reFileInfo_arr [$ j] ['name'] = $ doFile_arr [$ DoFile_num_int]; $ reFileInfo_arr [$ j] ['type'] = $ fileType_str; $ reFileInfo_arr [$ j] ['size'] = $ this-> change_size_express ($ fileSize_arr [$ I]) ;}} return $ reFileInfo_arr ;}}?> |