Hàm
2 dừng đọc tệp sau khi số byte củaCode language: PHP [php]
feof [ resource $stream ] : bool
8 đã được đọc hoặc đã đạt đến cuối tệp [EOF]Code language: PHP [php]
feof [ resource $stream ] : bool
Để kiểm tra xem con trỏ tệp có ở cuối tệp hay không, bạn có thể truyền nó cho hàm
4Code 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ó sẽ trả vềCode language: PHP [php]
feof [ resource $stream ] : bool
1Code language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
Để đọc từng dòng một tệp, bạn sử dụng hàm
9Code 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à tối đa một số byte để đọc. Nếu bạn bỏ qua đối sốCode language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
8, hàmCode language: PHP [php]
feof [ resource $stream ] : bool
2 sẽ đọc toàn bộ dòngCode language: PHP [php]
feof [ resource $stream ] : bool
Các ví dụ về tệp đọc PHP
Hãy lấy một số ví dụ về cách đọc tệp
1] Đọc toàn bộ tệp thành một chuỗi
Giả sử rằng bạn có một tệp tên là
44 nằm ở thư mụcCode language: PHP [php]
feof [ resource $stream ] : bool
45 với nội dung như sau
feof [ resource $stream ] : bool
Code language: PHP [php]
4Code language: PHP [php]
feof [ resource $stream ] : bool
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
44 thành một chuỗi và hiển thị trên trang webCode language: PHP [php]
feof [ resource $stream ] : bool
9Code language: PHP [php]
feof [ resource $stream ] : bool
Làm thế nào nó hoạt động
Đầu tiên, mở tệp
44 bằng hàmCode language: PHP [php]
feof [ resource $stream ] : bool
1Code language: PHP [php]
feof [ resource $stream ] : bool
2
fgets [ resource $handle , int $length = ? ] : string|false
Code language: PHP [php]
Thứ hai, đọc nội dung của toàn bộ tệp bằng hàm
2;Code language: PHP [php]
feof [ resource $stream ] : bool
5Code language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
Thứ ba, hiển thị nội dung của tệp trên trang web;
8Code language: PHP [php]
fgets [ resource $handle , int $length = ? ] : string|false
Cuối cùng, đóng tệp bằng hàm
3Code language: PHP [php]
feof [ resource $stream ] : bool
Lưu ý rằng hàm
95 là 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 tệpCode language: PHP [php]
feof [ resource $stream ] : bool
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ần đọc. Ví dụ sau sử dụng hàm
2 để đọc tối đa 100 byte từ tệpCode language: PHP [php]
feof [ resource $stream ] : bool
44Code language: PHP [php]
feof [ resource $stream ] : bool