Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of File Operation in php

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report