Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of File Operation in php

Shulou Source: shulou.com Published: 2022-06-03 20:23:23 09月25日 Update

This article will explain in detail the example analysis of file operation in php. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. Fstat function: displays all the information of the file

$file_path = "test.php"; if ($fp=fopen ($file_path, "a +")) {$file_info=fstat ($fp); echo "; print_r ($file_info); echo"; echo" file size is ". $file_info ['size']; echo" file last access time ".date (" Y-m-d H:i:s ", $file_info [' mtime']);} fclose ($fp); / / be sure to close

Second, file reading:

/ / first: $con = fread ($fp,filesize ($file_path)); $con = str_replace ("\ r\ n", "

", $con); echo" file content is ". $con;// the second kind: read all the files at once $con = file_get_contents ($file_path); $con = str_replace ("\ r\ n ","

", $con); echo" file content is ". $con;// third: segment-by-segment read $buffer = 1024; / / for the security of download, it is best to use the file byte read counter $file_count = 0 to determine whether the file has been 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 }

3. Write to the file:

/ / 1. The traditional method writes the file $file_path = "test.txt"; if (file_exists ($file_path)) {$fp = fopen ($file_path, "a +"); / / Open it: a + is the appended content. W + overwrites the original. $con = "Hello!\ r\ n"; fwrite ($fp,$con); echo "added successfully!" ;} else {echo "File does not exist";} fclose ($fp); / / 2. The second method is to write the file $file_path= "test.txt"; $con = "Hello to Beijing!\ r\ n"; file_put_contents ($file_path,$con,FILE_APPEND); echo "successful"

Fourth, the application of file operation:

/ / you can manipulate the ini file. Write the configuration of the server in the ini file, and then operate on it. Dbc.ini host=192.168.0.1 admin=admin password=123456 demo.php / / reads out the data in the ini file as an array and can be manipulated.

Copy files:

If (! copy ("E:\\ test.txt", "D:\\ 1.txt") {echo "fail";} else {echo "success";}

VI. Create a file

Create a folder:

/ / $path = "E:\\ happy"; / / folder path $path = "E:\\ happy\ aaa\ bbb"; / / Multi-level 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 files:

Delete a folder:

$path = "E:\\ happy\ aaa\ bbb"; / / Multi-level folder if (rmdir ($path)) {echo "success";} / / rmdir can only delete empty folders, files or directories under the folder cannot be deleted.

Delete the file:

$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" } this is the end of the article on "sample analysis of file operations in php". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Files folders content articles examples analysis success bytes methods more not bad practical secure below traditional hello information function size form Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft Apple OPPO Reno MySQL