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
1.
Code language: PHP [php]feof [ resource $stream ] : bool
- Đọc nội dung từ tệp bằng hàm
2.
Code language: PHP [php]feof [ resource $stream ] : bool
- Đóng tệp bằng hàm
3.
Code language: PHP [php]feof [ resource $stream ] : bool
Tại đây, cú pháp của hàm
2:Code language: PHP [php]
feof [ resource $stream ] : bool
Code language: PHP [php]
fread [ resource $stream , int $length ] : string|false
Hàm
2 có hai tham số:Code language: PHP [php]
feof [ resource $stream ] : bool
6 là tài nguyên con trỏ hệ thống tệp, thường là kết quả của hàmfeof [ resource $stream ] : bool
1.
Code language: PHP [php]feof [ resource $stream ] : bool
8 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ố
Code language: PHP [php]feof [ resource $stream ] : bool
8.
Code language: PHP [php]feof [ resource $stream ] : bool
Hàm
2 trả về nội dung tệp hoặcCode language: PHP [php]
feof [ resource $stream ] : bool
1 nếu không đọc.Code language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
Hàm
2 ngừng đọc tệp sau khi số lượng byteCode language: PHP [php]
feof [ resource $stream ] : bool
8 đã được đọc hoặc kết thúc của tệp [EOF] đã đạt được.Code language: PHP [php]
feof [ resource $stream ] : bool
Để kiểm tra xem con trỏ tệp ở cuối tệp, bạn có thể chuyển nó đến hàm
4:Code language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
Code language: PHP [php]
feof [ resource $stream ] : bool
Hàm
4 trả vềCode language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
6 nếuCode language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
6 ở EOF hoặc xảy ra lỗi. Nếu không, nó trả vềCode language: PHP [php]
feof [ resource $stream ] : bool
1.Code language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
Để đọc một dòng tệp từng dòng, bạn sử dụng hàm
9:Code language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
Code language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
Giống như hàm
2, hàmCode language: PHP [php]
feof [ resource $stream ] : bool
9 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ố
fgets [ resource $handle , int $length = ? ] : string|false
Code language: PHP [php]
8, hàmCode language: PHP [php]
feof [ resource $stream ] : bool
2 sẽ đọc toàn bộ dòng.Code language: PHP [php]
feof [ resource $stream ] : bool
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
4 được đặt tại thư mụcCode language: plaintext [plaintext]
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
5 với các nội dung sau:Code language: plaintext [plaintext]
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]
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
Ví dụ sau sử dụng hàm
2 để đọc nội dung của toàn bộ tệpCode language: PHP [php]
feof [ resource $stream ] : bool
4 thành một chuỗi và hiển thị nó trên trang web:Code language: plaintext [plaintext]
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