Php file operations ,. Php file operations, I. fstat function: display all information about the file $ file_pathtest.php; if ($ fpfopen ($ file_path, a +) {$ file_infofstat ($ fp); echopre; print _ PHP file operations,
I. fstat function: displays all information about a file.
$ File_path = "test. php"; if ($ fp = fopen ($ file_path, "a +") {$ file_info = fstat ($ fp); echo""; print_r($file_info); echo "
"; Echo" the file size is ". $ file_info ['size']; echo "last file access time ". date ("Y-m-d H: I: s", $ file_info ['mtime']);} fclose ($ fp); // be sure to disable
II. file reading:
// First: $ con = fread ($ fp, filesize ($ file_path); $ con = str_replace ("\ r \ n ","
", $ Con); echo" file content is ". $ con; // Type 2: Read all files $ con = file_get_contents ($ file_path); $ con = str_replace ("\ r \ n ","
", $ Con); echo" file content is ". $ con; // The third type: read from a segment $ buffer = 1024; // for download security, it is best to use the file byte read counter $ file_count = 0; // feof is used to determine whether the file is read to the end of the document while (! Feof ($ fp) & ($ file_size-$ file_count> 0) {$ file_data = fread ($ fp, $ buffer ); // count the number of bytes read $ file_count + $ buffer; echo $ file_data ;}
III. file writing:
// 1. write the file $ file_path = "test.txt" in the traditional method; if (file_exists ($ file_path) {$ fp = fopen ($ file_path, "a + "); // Open Method: a + is the append content. W + overwrites the original one. $ Con = "Hello! \ R \ n "; fwrite ($ fp, $ con); echo" added successfully! ";}Else {echo" the file does not exist ";} fclose ($ fp); // 2. write the file $ file_path =" test.txt "in the second method "; $ con = "Hello Beijing! \ R \ n "; file_put_contents ($ file_path, $ con, FILE_APPEND); echo" success ";
IV. Application of file operations:
// You can operate on the INI file. Write the server configuration in the INI file and then operate on it. Dbc. ini host = 192.168.0.1 admin = admin password = 123456 demo. php
// Read the data in the INI file as an array and perform operations on it.
5. copy an object:
if(!copy("E:\\test.txt","D:\\1.txt")){ echo "fail";}else{ echo "success";}
6. create a file
Create a folder:
// $ Path = "E: \ happy"; // folder path $ path = "E: \ happy \ aaa \ bbb"; // multilevel folder if (! Is_dir ($ path) {if (mkdir ($ path, 0777, true) {echo "success" ;}else {echo "fail ";}} else {echo "folder already exists ";}
Create a file:
$file_path = "E:\\happy.txt";$fp = fopen($file_path,"w+");fwrite($fp,"hello");fclose($fp);
7. delete an object:
Delete a folder:
$ Path = "E :\\ happy \ aaa \ bbb"; // multilevel folder if (rmdir ($ path) {echo "success ";} // rmdir: only empty folders can be deleted. files or directories in folders cannot be deleted.
Delete an object:
$ File_path = "E :\\ happy.txt"; if (is_file ($ file_path) {if (unlink ($ file_path) {echo "success ";} else {echo "fail" ;}} else {echo "file does not exist ";}
Functions, 1. fstat function: displays all information about the file $ file_path = "test. php "; if ($ fp = fopen ($ file_path," a + ") {$ file_info = fstat ($ fp); echo" pre "; print _...