Hướng dẫn file handling in php - xử lý tệp trong php



Phần này hướng dẫn bạn xử lý file trong PHP, PHP cho phép chúng ta tạo file, đọc từng dòng của file, đọc file theo ký tự, ghi file, nối thêm nội dung vào file, xóa file và đóng file.


Nội dung chính

  • Mở file - hàm PHP fopen()
  • Đóng file - hàm PHP fclose()
  • Đọc file - hàm PHP fcfreadlose()
  • Ghi file - hàm PHP fwrite()
  • Xóa file - hàm PHP unlink()

Mở file - hàm PHP fopen()

Hàm PHP fopen() được sử dụng để mở một file.

Cú pháp:

fopen (string $filename , string $mode [, bool $use_include_path = false [, resource $context ]])

Ví dụ:



Đóng file - hàm PHP fclose()

Hàm PHP fclose() được sử dụng để đóng một file.

Cú pháp:

Ví dụ:


Đóng file - hàm PHP fclose()

Hàm PHP fclose() được sử dụng để đóng một file.

Cú pháp:

string fread(resource $handle, int $length )

Ví dụ:

    

Đóng file - hàm PHP fclose()



Hàm PHP fclose() được sử dụng để đóng một file.

Đọc file - hàm PHP fcfreadlose()

Cú pháp:

int fwrite(resource $handle , string $string [, int $length ])

Ví dụ:

  

Đóng file - hàm PHP fclose()

File được ghi thành công.


Hàm PHP fclose() được sử dụng để đóng một file.

Đọc file - hàm PHP fcfreadlose()

Cú pháp:

bool unlink ( string $filename [, resource $context ])

Ví dụ:

Đóng file - hàm PHP fclose()

0




Xử lý tập tin là một phần quan trọng của bất kỳ ứng dụng web nào. Bạn thường cần mở và xử lý một tệp cho các tác vụ khác nhau.

Nội dung chính ShowShow

  • Php thao tác các tập tin
  • Hàm readfile ()
  • Bài tập PHP
  • Xử lý tệp trong PHP: Mở tệp
  • Xử lý tệp trong PHP: Đóng tệp
  • Đọc tập tin
  • Viết tập tin
  • readfile()8 $file= fopen("demo.txt", "r"); 0 $file= fopen("demo.txt", "r"); 1 $file= fopen("demo.txt", "r"); 2 $file= fopen("demo.txt", "r"); 3 ?>
  • Việc xử lý tệp trong PHP là gì?
  • PHP có thể đọc từ tập tin không?
  • Có bao nhiêu chế độ xử lý tệp trong PHP?
  • PHP có thể ghi vào tệp không?


Php thao tác các tập tin

Hàm readfile ()

Bài tập PHP

Xử lý tệp trong PHP: Mở tệp

Xử lý tệp trong PHP: Đóng tệp


Hàm readfile ()

Bài tập PHP

Xử lý tệp trong PHP: Mở tệp

Xử lý tệp trong PHP: Đóng tệp
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

Đọc tập tin

Viết tập tin

readfile()8 $file= fopen("demo.txt", "r"); 0 $file= fopen("demo.txt", "r"); 1 $file= fopen("demo.txt", "r"); 2 $file= fopen("demo.txt", "r"); 3 ?>



Bài tập PHP

Xử lý tệp trong PHP: Mở tệp

  • Xử lý tệp trong PHP: Đóng tệp
  • Đọc tập tin
  • Đọc tập tin
  • Viết tập tin
  • readfile()8 $file= fopen("demo.txt", "r"); 0 $file= fopen("demo.txt", "r"); 1 $file= fopen("demo.txt", "r"); 2 $file= fopen("demo.txt", "r"); 3 ?>

Xử lý tệp trong PHP: Mở tệp

Xử lý tệp trong PHP: Đóng tệp

Example:

Đọc tập tin

5
6
7

Viết tập tin

readfile()8 $file= fopen("demo.txt", "r"); 0 $file= fopen("demo.txt", "r"); 1 $file= fopen("demo.txt", "r"); 2 $file= fopen("demo.txt", "r"); 3 ?> Việc xử lý tệp trong PHP là gì?
PHP có một số chức năng để tạo, đọc, tải lên và chỉnh sửa các tệp. Hãy cẩn thận khi thao tác các tập tin!
r+ Khi bạn đang thao túng các tập tin, bạn phải rất cẩn thận.
Bạn có thể gây ra rất nhiều thiệt hại nếu bạn làm điều gì đó sai. Các lỗi phổ biến là: Chỉnh sửa tệp sai, điền vào ổ cứng bằng dữ liệu rác và xóa nội dung của một tệp một cách tình cờ. Hàm
1 đọc một tệp và ghi nó vào bộ đệm đầu ra.
w+ Giả sử chúng tôi có một tệp văn bản có tên là "WebDictionary.txt", được lưu trữ trên máy chủ, trông như thế này:
Ajax = javaScript và xmlcss không đồng bộ = bảng kiểu cascading html = hyper text markup ngôn ngữCSS = Cascading Style Sheets HTML = Hyper Text Markup LanguagePHP = PHP Hypertext PreprocessorSQL = Structured Query LanguageSVG = Scalable Vector GraphicsXML = EXtensible Markup Language Mã PHP để đọc tệp và ghi nó vào bộ đệm đầu ra như sau (hàm
1 trả về số byte đọc trên thành công):
a+ Hàm
1 rất hữu ích nếu tất cả những gì bạn muốn làm là mở một tệp và đọc nội dung của nó.
Các chương tiếp theo sẽ dạy bạn nhiều hơn về xử lý tập tin. Trong thực tế, các tập lệnh PHP cần làm việc với dữ liệu được truy xuất từ ​​các tệp đĩa, bộ kết quả SQL, tài liệu XML và nhiều loại dữ liệu khác. PHP có nhiều chức năng tích hợp để truy cập các nguồn dữ liệu này. Hãy để hiểu về việc xử lý tệp trong PHP theo thứ tự sau:
x+ Mở tập tin

Xử lý tệp trong PHP: Đóng tệp

Đọc tập tin

NOTE:   

  • Viết tập tin

  • readfile()8 $file= fopen("demo.txt", "r"); 0 $file= fopen("demo.txt", "r"); 1 $file= fopen("demo.txt", "r"); 2 $file= fopen("demo.txt", "r"); 3 ?>

Example:

Việc xử lý tệp trong PHP là gì?

2
3
10
7

PHP có một số chức năng để tạo, đọc, tải lên và chỉnh sửa các tệp.

Đọc tập tin

API thao tác tệp PHP, cực kỳ linh hoạt: nó cho phép bạn đọc các tệp vào một chuỗi hoặc thành một mảng, từ hệ thống tệp cục bộ hoặc một URL từ xa, theo các dòng, byte hoặc ký tự.

  • fread (): hàm fread () được sử dụng để đọc nội dung của dữ liệu. Tham số đầu tiên là con trỏ tệp và cái còn lại là kích thước tệp tính bằng byte.: The fread() function is used to read the contents of the data. The first parameter is the file pointer and the other is the file size in bytes.: The fread() function is used to read the contents of the data. The first parameter is the file pointer and the other is the file size in bytes.

4 
13 
14 
15 
16 
7 
13 
14 
15 
16 
7
13
14
15
16
7

  • Hàm Feof () có thể được sử dụng để kiểm tra xem có đạt được kết thúc của File (EOF) hay không. Điều này rất hữu ích vì chúng ta có thể lặp qua một tệp có độ dài không xác định. Chúng tôi có thể làm điều này trên mọi tệp trong bất kỳ chế độ nào. function can be used to check if the “End-Of-File” (EOF) has been reached. This is very useful as we can loop through a file of unknown length. We can do this on every file in any mode. function can be used to check if the “End-Of-File” (EOF) has been reached. This is very useful as we can loop through a file of unknown length. We can do this on every file in any mode.

18 
19 
10 
11 
12 
13 
14 
7 
19 
10 
11 
12 
13 
14 
7
19
10
11
12
13
14
7

  • fgetc (): hàm fgetc () được sử dụng để đọc một ký tự từ một tệp. The fgetc() function is used to read a character from a file. The fgetc() function is used to read a character from a file.

16 
17 
10 
11 
40 
13 
14 
7 
17 
10 
11 
40 
13 
14 
7
17
10
11
40
13
14
7

  • file_get_contents (): Để đọc các tệp đĩa cục bộ trong hàm file_get_contents () được sử dụng. Hàm này chấp nhận tên và đường dẫn của tệp đĩa và đọc toàn bộ tệp thành một biến chuỗi cùng một lúc. To read the local disk files in PHP, file_get_contents() function is used. This function accepts the name and path of a disk file, and reads the entire file into a string variable at once. To read the local disk files in PHP, file_get_contents() function is used. This function accepts the name and path of a disk file, and reads the entire file into a string variable at once.

18 
45 
46 
47 
7 
45 
46 
47 
7
45
46
47
7

  • File (): Một phương thức đọc dữ liệu thay thế từ tệp là hàm Php File (). Nó chấp nhận tên và đường dẫn của tệp và đọc toàn bộ tệp vào một mảng, có từng phần tử của mảng đại diện cho một dòng của tệp. Chúng tôi lặp lại thông qua mảng bằng cách sử dụng vòng lặp foreach để truy cập vào các phần tử của mảng. An alternative method of reading data from a file is PHP’s file() function. It accepts the name and path of the file and reads the entire file into an array, which has each element of the array representing one line of the file. We iterate through the array using foreach loop to access the elements of the array. An alternative method of reading data from a file is PHP’s file() function. It accepts the name and path of the file and reads the entire file into an array, which has each element of the array representing one line of the file. We iterate through the array using foreach loop to access the elements of the array.

18 
50 
51 
52 
11 
54 
13 
7 
50 
51 
52 
11 
54 
13 
7
50
51
52
11
54
13
7

  • Đọc các tệp từ xa: Cả File_Get_Contents () và File () hỗ trợ đọc dữ liệu từ URL, sử dụng giao thức HTTP hoặc FTP. Nó đọc một tệp HTML của web thành một mảng. Both file_get_contents() and file() support reading data from URLs, using either the HTTP or FTP protocol. It reads an HTML file of the Web into an array. Both file_get_contents() and file() support reading data from URLs, using either the HTTP or FTP protocol. It reads an HTML file of the Web into an array.

18 
58 
59 
52 
11 
54 
13 
7 
58 
59 
52 
11 
54 
13 
7
58
59
52
11
54
13
7

Lưu ý: Trong trường hợp các liên kết mạng chậm, việc đọc một tệp từ xa trong các đoạn, đó là hiệu quả hơn, để tối đa hóa hiệu quả của băng thông mạng có sẵn. Hàm fgets () có thể đọc một số byte cụ thể từ một tệp.In case of slow network links, it is more efficient to read a remote file in “chunks,” to maximize the efficiency of available network bandwidth. fgets() function can read a specific number of bytes from a file.In case of slow network links, it is more efficient to read a remote file in “chunks,” to maximize the efficiency of available network bandwidth. fgets() function can read a specific number of bytes from a file.

18 
66 
67 
68 
10 
11 
71 
72 
73 
14 
47 
76 
66 
67 
68 
10 
11 
71 
72 
73 
14 
47 
7
66
67
68
10
11
71
72
73
14
47
7

Viết tập tin

  • fwrite (): hàm & nbsp; fwrite () & nbsp; hàm được sử dụng để ghi vào một tệp. Tham số đầu tiên của & nbsp; fwrite () & nbsp; chứa tên của tệp để ghi và tham số thứ hai là chuỗi được viết.The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.

18 
78 
79 
40 
14 
7 
78 
79 
40 
14 
7
78
79
40
14
7

  • Ghi đè PHP: Demo demo.txt có chứa một số dữ liệu. Tất cả các dữ liệu hiện có sẽ bị xóa và chúng tôi bắt đầu với một tệp trống. “demo.txt” contains some data. All the existing data will be ERASED and we start with an empty file. “demo.txt” contains some data. All the existing data will be ERASED and we start with an empty file.

18 
78 
79 
40 
14 
7 
78 
79 
40 
14 
7
78
79
40
14
7

  • Ghi đè PHP: Demo demo.txt có chứa một số dữ liệu. Tất cả các dữ liệu hiện có sẽ bị xóa và chúng tôi bắt đầu với một tệp trống. The file_put_contents() function accepts a filename and the data to be written to the file. The file_put_contents() function accepts a filename and the data to be written to the file.

file_put_contents (): hàm file_put_contents () chấp nhận tên tệp và dữ liệu sẽ được ghi vào tệp.

20 
21 
22 
23 
7
20
21
22
23
7

readfile()8 $file= fopen("demo.txt", "r"); 0 $file= fopen("demo.txt", "r"); 1 $file= fopen("demo.txt", "r"); 2 $file= fopen("demo.txt", "r"); 3 ?>

  • Các hoạt động xử lý tệp và thư mục khác The filesize() function calculates the size of a file in bytes. It takes file name as an argument. The filesize() function calculates the size of a file in bytes. It takes file name as an argument.

Tính toán kích thước tệp: Hàm fileSize () Tính toán kích thước của tệp tính bằng byte. Nó lấy tên tệp làm đối số.

26 
27 
28 
29 
30 
13 
7
26
27
28
29
30
13
7

  • 18 
    26 
    27 
    28 
    29 
    30 
    13 
    7 The realpath() function is used to retrieve the absolute file path to a file.
    The realpath() function is used to retrieve the absolute file path to a file.

Tìm đường dẫn tệp tuyệt đối: hàm realpath () được sử dụng để truy xuất đường dẫn tệp tuyệt đối đến một tệp.

34 
27 
36 
29 
30 
13 
7
34
27
36
29
30
13
7

18 
34 
27 
36 
29 
30 
13 
7 We can also use the pathinfo() function. It returns an array containing the file’s path, name, and extension.
We can also use the pathinfo() function. It returns an array containing the file’s path, name, and extension.

Lưu ý: Chúng tôi cũng có thể sử dụng hàm pathinfo (). Nó trả về một mảng chứa đường dẫn, tên và phần mở rộng của tệp.

102 
27 
104 
29 
30 
13 
7
102
27
104
29
30
13
7

  • 18 
    102 
    27 
    104 
    29 
    30 
    13 
    7We can get detailed information about a particular file, including its ownership, permissions, and modification and access times, using stat() function. It returns this information as an associative array.
    We can get detailed information about a particular file, including its ownership, permissions, and modification and access times, using stat() function. It returns this information as an associative array.

Truy xuất các thuộc tính tệp: Chúng tôi có thể nhận được thông tin chi tiết về một tệp cụ thể, bao gồm quyền sở hữu, quyền và thời gian sửa đổi và truy cập của nó, sử dụng hàm stat (). Nó trả về thông tin này như một mảng kết hợp.

110 
27 
112 
29 
30 
13 
7
110
27
112
29
30
13
7

  • 18 
    110 
    27 
    112 
    29 
    30 
    13 
    7 We can check if a file is readable, writable or executable with the help of is_readable(), is_writable(), and is_executable() functions.
    We can check if a file is readable, writable or executable with the help of is_readable(), is_writable(), and is_executable() functions.

Kiểm tra các chế độ: Chúng tôi có thể kiểm tra xem một tệp có thể đọc được, có thể ghi hoặc thực thi với sự trợ giúp của các hàm is_readable (), is_writable () và is_executable ().

110 
119 
27 
121 
122 
123 
124 
13 
126 
127 
128 
13 
130 
131 
132 
13 
29 
30 
13 
7
110
119
27
121
122
123
124
13
126
127
128
13
130
131
132
13
29
30
13
7

  • Kiểm tra xem nó có phải là tệp hay không: hàm is_dir () trả về true nếu đối số được truyền cho nó là một thư mục. Hàm is_file () trả về true nếu đối số được truyền cho nó là một tệp.The is_dir() function returns true if the argument passed to it is a directory. The is_file() function returns true if the argument passed to it is a file.The is_dir() function returns true if the argument passed to it is a directory. The is_file() function returns true if the argument passed to it is a file.

18 
139 
27 
141 
142 
13 
144 
145 
13 
7 
139 
27 
141 
142 
13 
144 
145 
13 
7
139
27
141
142
13
144
145
13
7

  • Sao chép tệp: Hàm Copy () sao chép tệp từ vị trí này sang vị trí khác. Nó có nguồn tệp và đường dẫn đích như hai đối số The copy() function copies a file from one location to another location. It has the file source and destination path as two arguments The copy() function copies a file from one location to another location. It has the file source and destination path as two arguments

18 
149 
27 
151 
152 
29 
154 
13 
29 
145 
13 
7 
149 
27 
151 
152 
29 
154 
13 
29 
145 
13 
7
149
27
151
152
29
154
13
29
145
13
7

  • Đổi tên các tệp hoặc thư mục: hàm đổi tên () được sử dụng để đổi tên hoặc di chuyển một tệp (hoặc thư mục). Tôi lấy tên đường dẫn cũ và mới làm đối số. The rename() function is used to rename or movee a file (or directory). I takes the old and new path names as arguments. The rename() function is used to rename or movee a file (or directory). I takes the old and new path names as arguments.

18 
161 
27 
163 
164 
29 
166 ____23 
29 
145 
13 
161 
27 
163 
164 
29 
166 
13 
29 
145 
13 
171 
172 
173 
174 
29 
176 
13 
29 
179 
13 
7
161
27
163
164
29
166
13
29
145
13
171
172
173
174
29
176
13
29
179
13
7

  • Xóa các tệp hoặc thư mục: hàm hủy liên kết () được sử dụng để xóa một tệp. Nó lấy tên tệp làm đối số. The unlink() function is used to remove a file. It takes the filename name as an argument. The unlink() function is used to remove a file. It takes the filename name as an argument.

18 
183 
27 
185 
186 
29 
188 
13 
29 
145 
13 
7 
183 
27 
185 
186 
29 
188 
13 
29 
145 
13 
7
183
27
185
186
29
188
13
29
145
13
7

  • Khóa và mở khóa các tệp: Hàm Flock () khóa một tệp trước khi đọc hoặc ghi nó để không thể truy cập được bởi một quy trình khác. Thực hiện điều này làm giảm khả năng tham nhũng dữ liệu có thể xảy ra nếu hai quá trình cố gắng ghi dữ liệu khác nhau vào cùng một tệp ngay lập tức. Tham số đầu tiên lấy tên tệp. Tham số thứ hai cho Flock () Chỉ định loại khóa: Lock_ex tạo khóa độc quyền để viết, Lock_SH tạo khóa không độc quyền để đọc và Lock_un phá hủy khóa. The flock() function locks a file before reading or writing it so that it cannot be accessed by another process. Doing this reduces the possibility of data corruption that might occur if two processes attempt to write different data to the same file at the same instant. The first parameter takes the file name. The second parameter to flock() specifies the type of lock: LOCK_EX creates an exclusive lock for writing, LOCK_SH creates a non-exclusive lock for reading, and LOCK_UN destroys the lock. The flock() function locks a file before reading or writing it so that it cannot be accessed by another process. Doing this reduces the possibility of data corruption that might occur if two processes attempt to write different data to the same file at the same instant. The first parameter takes the file name. The second parameter to flock() specifies the type of lock: LOCK_EX creates an exclusive lock for writing, LOCK_SH creates a non-exclusive lock for reading, and LOCK_UN destroys the lock.

18 
195 
20 
197 
198 
199 
100 
101 
102 
14 
23 
195 
20 
197 
198 
199 
100 
101 
102 
14 
23 
7
195
20
197
198
199
100
101
102
14
23
7

Đọc và viết tệp là các nhiệm vụ cơ bản mà bất kỳ nhà phát triển PHP nào cũng nên biết cách hoàn thành; Bài viết này bao gồm cả hai nhiệm vụ này, và nhiều hơn nữa. Nó bắt đầu với một minh chứng về cách đọc các tệp cục bộ và từ xa, cũng như cách tạo các tệp mới hoặc nối dữ liệu vào các tệp hiện có. Sau đó, nó chuyển sang thao tác thư mục theo PHP và giải thích nhiều chức năng thư mục và tệp PHP.

Với điều này, chúng tôi đã kết thúc việc xử lý tập tin này trong bài viết của PHP. Tôi hy vọng xử lý tập tin không nên là một vấn đề. Kiểm tra & nbsp; đào tạo chứng nhận PHP & nbsp; bởi Edureka, & nbsp;heck out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.heck out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Có một câu hỏi cho chúng tôi? Vui lòng đề cập đến nó trong phần bình luận của xử lý tập tin trong Php và tôi sẽ liên hệ lại với bạn.

Việc xử lý tệp trong PHP là gì?

Xử lý tệp trong PHP: Mở tệp hàm fopen () được sử dụng để mở tệp.Tham số đầu tiên chứa tên của tệp sẽ được mở và tham số thứ hai cho biết tệp sẽ được mở trong chế độ nào.Hàm fopen () tạo ra một con trỏ trỏ đến tệp được đề cập.The fopen() function is used to open a file. The first parameter contains the name of the file to be opened and the second parameter tells in which mode the file should be opened. fopen() function creates a pointer which points to the file mentioned.The fopen() function is used to open a file. The first parameter contains the name of the file to be opened and the second parameter tells in which mode the file should be opened. fopen() function creates a pointer which points to the file mentioned.

PHP có thể đọc từ tập tin không?

Php Read File - fread () hàm fread () đọc từ một tệp mở.Tham số đầu tiên của fread () chứa tên của tệp để đọc và tham số thứ hai chỉ định số lượng byte tối đa để đọc.The fread() function reads from an open file. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.The fread() function reads from an open file. The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

Có bao nhiêu chế độ xử lý tệp trong PHP?

Trong xử lý tệp PHP, có bốn bộ chế độ có thể.Đó là, {r và r+} - để đọc các tệp hiện có.four sets of possible modes. These are, {r and r+} – To read the existing files.four sets of possible modes. These are, {r and r+} – To read the existing files.

PHP có thể ghi vào tệp không?

PHP ghi vào tệp - fwrite () hàm fwrite () được sử dụng để ghi vào tệp.Tham số đầu tiên của fwrite () chứa tên của tệp để ghi vào và tham số thứ hai là chuỗi được viết.The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.The fwrite() function is used to write to a file. The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.