PHP cung cấp các chức năng khác nhau để đọc dữ liệu từ tệp. Có nhiều chức năng khác nhau cho phép bạn đọc tất cả dữ liệu tệp, đọc từng dòng dữ liệu và đọc ký tự dữ liệu theo ký tự.
Các chức năng đọc tệp PHP có sẵn được đưa ra dưới đây.
- fread()
- fgets()
- fgetc()
Php Read File - fread ()
Hàm php fread () được sử dụng để đọc dữ liệu của tệp. Nó yêu cầu hai đối số: tài nguyên tệp và kích thước tệp.
Cú pháp
$ xử lý đại diện cho con trỏ tệp được tạo bởi hàm fopen (). represents file pointer that is created by fopen() function.
chiều dài $ đại diện cho độ dài của byte được đọc. represents length of byte to be read.
Thí dụ
Đầu ra
this is first line this is another line this is third line
Php Read File - Fgets ()
Hàm php fget () được sử dụng để đọc một dòng từ tệp.
Cú pháp
Thí dụ
Đầu ra
Php Read File - Fgets ()
Hàm php fget () được sử dụng để đọc một dòng từ tệp.
Cú pháp
Thí dụ
Đầu ra
this is first line this is another line this is third line
Tóm tắt: Trong hướng dẫn này, bạn sẽ học cách đọc một tệp bằng các hàm PHP tích hợp khác nhau.: in this tutorial, you’ll learn how to read a file using the various built-in PHP functions.
Để đọc nội dung từ một tệp, bạn làm theo các bước sau:
- Mở tệp để đọc bằng hàm this is first line this is another line this is third line 3.
- Đọc nội dung từ tệp bằng hàm this is first line this is another line this is third line 4.
- Đóng tệp bằng hàm this is first line this is another line this is third line 5.
Tại đây, cú pháp của hàm this is first line this is another line this is third line 4:
fread ( resource $stream , int $length ) : string|false
Code language: PHP (php)Hàm this is first line this is another line this is third line 4 có hai tham số:
- this is first line this is another line this is third line 8 là tài nguyên con trỏ hệ thống tệp, thường là kết quả của hàm this is first line this is another line this is third line 3.
fread ( resource $stream , int $length ) : string|false
Code language: PHP (php)0 chỉ định số lượng byte tối đa để đọc. Nếu bạn muốn đọc toàn bộ tệp, bạn có thể chuyển kích thước tệp cho tham sốfread ( resource $stream , int $length ) : string|false
Code language: PHP (php)0.
Hàm this is first line this is another line this is third line 4 trả về nội dung tệp hoặc
fread ( resource $stream , int $length ) : string|false
Code language: PHP (php)3 nếu không đọc.Hàm this is first line this is another line this is third line 4 dừng đọc tệp sau khi số byte
fread ( resource $stream , int $length ) : string|false
Code language: PHP (php)0 đã được đọc hoặc kết thúc của tệp (EOF) đã đạt được.Để kiểm tra xem con trỏ tệp ở cuối tệp, bạn có thể chuyển nó đến hàm
fread ( resource $stream , int $length ) : string|false
Code language: PHP (php)6:feof ( resource $stream ) : bool
Code language: PHP (php)Hàm
fread ( resource $stream , int $length ) : string|false
Code language: PHP (php)6 trả vềfread ( resource $stream , int $length ) : string|false
Code language: PHP (php)8 nếu this is first line this is another line this is third line 8 ở EOF hoặc xảy ra lỗi. Nếu không, nó trả vềfread ( resource $stream , int $length ) : string|false
Code language: PHP (php)3.Để đọc một dòng tệp từng dòng, bạn sử dụng hàm
feof ( resource $stream ) : bool
Code language: PHP (php)1:fgets ( resource $handle , int $length = ? ) : string|false
Code language: PHP (php)Giống như hàm this is first line this is another line this is third line 4, hàm
feof ( resource $stream ) : bool
Code language: PHP (php)1 chấp nhận tài nguyên con trỏ hệ thống tệp và lên đến một số byte để đọc. Nếu bạn bỏ qua đối sốfread ( resource $stream , int $length ) : string|false
Code language: PHP (php)0, hàm this is first line this is another line this is third line 4 sẽ đọc toàn bộ dòng.Php đọc ví dụ về tệp
Hãy cùng lấy một số ví dụ về cách đọc một tập tin.
1) Đọc toàn bộ tệp vào một chuỗi
Giả sử rằng bạn có một tệp có tên
feof ( resource $stream ) : bool
Code language: PHP (php)6 được đặt tại thư mụcfeof ( resource $stream ) : bool
Code language: PHP (php)7 với các nội dung sau:1 New York New York 8,253,213 2 Los Angeles California 3,970,219 3 Chicago Illinois 2,677,643 4 Houston Texas 2,316,120 5 Phoenix Arizona 1,708,127 6 Philadelphia Pennsylvania 1,578,487 7 San Antonio Texas 1,567,118 8 San Diego California 1,422,420 9 Dallas Texas 1,343,266 10 San Jose California 1,013,616
Code language: plaintext (plaintext)Ví dụ sau sử dụng hàm this is first line this is another line this is third line 4 để đọc nội dung của toàn bộ tệp
feof ( resource $stream ) : bool
Code language: PHP (php)6 thành một chuỗi và hiển thị nó trên trang web:<?php $filename = './public/population.txt'; $f = fopen($filename, 'r'); if ($f) { $contents = fread($f, filesize($filename)); fclose($f); echo nl2br($contents); }
Code language: HTML, XML (xml)Làm thế nào nó hoạt động.
Đầu tiên, hãy mở tệp
feof ( resource $stream ) : bool
Code language: PHP (php)6 bằng hàm this is first line this is another line this is third line 3:$f = fopen($filename, 'r');
Code language: PHP (php)Thứ hai, đọc nội dung của toàn bộ tệp bằng hàm this is first line this is another line this is third line 4; Sử dụng chức năng
fgets ( resource $handle , int $length = ? ) : string|false
Code language: PHP (php)3 để có được kích thước của tệp:$contents = fread($f, filesize($filename));
Code language: PHP (php)Thứ ba, hiển thị nội dung của tệp trên một trang web; Sử dụng chức năng
fgets ( resource $handle , int $length = ? ) : string|false
Code language: PHP (php)4 để chuyển đổi các ký tự mới thành thẻfgets ( resource $handle , int $length = ? ) : string|false
Code language: PHP (php)5.echo nl2br($contents);
Code language: PHP (php)Cuối cùng, đóng tệp bằng hàm this is first line this is another line this is third line 5.
Lưu ý rằng hàm
fgets ( resource $handle , int $length = ? ) : string|false
Code language: PHP (php)7 là một phím tắt để mở tệp, đọc toàn bộ nội dung của tệp thành một chuỗi và đóng nó.2) Đọc một số ký tự từ một tệp
Để đọc một số ký tự từ một tệp, bạn chỉ định số byte để đọc. Ví dụ sau sử dụng hàm this is first line this is another line this is third line 4 để đọc tối đa 100 byte từ tệp
feof ( resource $stream ) : bool
Code language: PHP (php)6:this is first line this is another line this is third line 0Output:
this is first line this is another line this is third line 13) Đọc từng dòng tệp
Ví dụ sau sử dụng hàm
feof ( resource $stream ) : bool
Code language: PHP (php)1 để đọc dòng tệpfeof ( resource $stream ) : bool
Code language: PHP (php)6 theo từng dòng: this is first line this is another line this is third line 2Bản tóm tắt
- Sử dụng chức năng this is first line this is another line this is third line 4 để đọc một số hoặc tất cả nội dung từ một tệp.
- Sử dụng chức năng
feof ( resource $stream ) : bool
Code language: PHP (php)1 để đọc một dòng từ một tệp. - Sử dụng hàm
fread ( resource $stream , int $length ) : string|false
Code language: PHP (php)6 để kiểm tra phần cuối đã đạt được. - Sử dụng chức năng
fgets ( resource $handle , int $length = ? ) : string|false
Code language: PHP (php)3 để có được kích thước của tệp.
Bạn có thấy hướng dẫn này hữu ích không?