PHP learning notes-PHP file operations,

Source: Internet
Author: User

PHP learning notes-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" <pre> "; print_r ($ file_info); echo "</pre>"; echo "file size ". $ 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:

// Type 1: $ con = fread ($ fp, filesize ($ file_path); $ con = str_replace ("\ r \ n", "<br> ", $ con); echo "file content is ". $ con; // type 2: Read all files $ con = file_get_contents ($ file_path); $ con = str_replace ("\ r \ n", "<br> ", $ 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 <? Php $ con = parse_ini_file ("dbc. ini"); print_r ($ con);?> // 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 ";}

The above is a small Editor to introduce you to PHP file operations related knowledge, I hope to help you.

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.