In PHP provides a large number of functions of the file operation, I will give you to summarize the PHP file function on the file read, write operations, have to understand these functions of friends can enter the reference.
One, the operation file, obtains the file information
The code is as follows |
Copy Code |
Open File $file _path= "Text.txt"; if ($fp =fopen ($file _path, "R")) { Information for removing files $file _info=fstat ($FP); echo " "; Print_r ($file _info); echo " ";Single Take-out $file _size= $file _info[' size '; The file size is calculated in bytes echo "File Size:". $file _size; echo " Time the file was last accessed: ". Date (" Y-m-d h:i:s ", $file _info[' atime ']); Atime "The time the file was last accessed" echo " Time the file was last modified: ". Date (" Y-m-d h:i:s ", $file _info[' mtime ']); Mtime "time when the file last content was modified" echo " File last change time: ". Date (" Y-m-d h:i:s ", $file _info[' CTime ']); CTime indicates "time when the file was last file owner/filegroup Modified" }else{ echo "Failed to open file"; }
Close the file, this is very important Fclose ($FP); ?> |
2. The second way to obtain the information of the file
The code is as follows |
Copy Code |
The second way to get file information $file _path= "Text.txt"; if (!file_exists ($file _path)) { echo "file does not exist"; Exit (); } echo " ". Date (" Y-m-d h:i:s ", Fileatime ($file _path)); echo " ". Date (" Y-m-d h:i:s ", Filemtime ($file _path)); echo " ". Date (" Y-m-d h:i:s ", Filectime ($file _path)); echo " ". Filemtime ($file _path); echo " ". Filectime ($file _path); ?> |
Second, read the file operation
The code is as follows |
Copy Code |
Read file $file _path= "Text.txt"; if (!file_exists ($file _path)) { echo "file does not exist"; Exit (); }
Open File $FP =fopen ($file _path, "A +"); Read file $content =fread ($fp, FileSize ($file _path)); echo "File content is: "; By default, the content is output to a Web page and will not be wrapped because the page does not recognize RN. All to the RN =
$content =str_replace ("RN", " ", $content); Echo $content; Fclose ($FP); ?>
|
2, the second way to read the file
The code is as follows |
Copy Code |
The second way to read the file
$file _path= "Text.txt"; if (!file_exists ($file _path)) { echo "file does not exist"; Exit (); } $content =file_get_contents ($file _path); $content =str_replace ("RN", " ", $content); Echo $content; ?>
|
3, the third method of reading, cyclic reading (against large files)
The code is as follows |
Copy Code |
Third reading method, cyclic reading (against large files)
$file _path= "Text.txt"; if (!file_exists ($file _path)) { echo "file does not exist"; Exit (); } Open File $FP =fopen ($file _path, "A +"); Define the number of bytes per read $buffer = 1024; While reading. To determine if the end of the file is reached while (!feof ($fp)) { Read data by 1024 bytes $content =fread ($fp, $buffer); Echo $content; } Fclose ($FP); ?>
|
4, file read the actual application: When we connect to the database, the specified data can be configured to a file, and then PHP runtime, real-time access to information
The code is as follows |
Copy Code |
Db.ini file host=127.0.0.1 User=root Pwd=root Db=test |
Get file
The code is as follows |
Copy Code |
$arr =parse_ini_file ("Db.ini"); echo " "; Print_r ($arr); echo " ";
echo $arr [' Host '];Connecting to a database $conn = mysql_connect ($arr [' Host '], $arr [' User '], $arr [' pwd ']; if (! $conn) { echo "Error"; } echo "OK"; ?> |
Third, write the document
The code is as follows |
Copy Code |
Write a file $file _path= "Text.txt"; if (!file_exists ($file _path)) { echo "file does not exist"; Exit (); } "A +" appends "w+" to the file to write back $FP =fopen ($file _path, "w+"); $con = "rn Hello"; for ($i =0; $i <10; $i + +) { Fwrite ($fp, $con);} echo "Add success"; Fclose ($FP); ?>
|
2, the second way through the file_put_contents function
The code is as follows |
Copy Code |
The second way to write files $file _path= "Text.txt"; $content = "HELLO,WORLDRN";
Writing a string to a file by default is "File_use_include_path" "w+" re-written File_put_contents ($file _path, $content, file_append); echo "OK"; ?> |
http://www.bkjia.com/PHPjc/445636.html www.bkjia.com true http://www.bkjia.com/PHPjc/445636.html techarticle in PHP provides a large number of functions of the file operation, I will give you to summarize the PHP file function on the file read, write operations, have to understand these functions of friends can enter the reference. ...