Hướng dẫn how will you read a file in php? - làm thế nào bạn sẽ đọc một tập tin trong php?

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

    feof [ resource $stream ] : bool

    Code language: PHP [php]
    1.
  • Đọc nội dung từ tệp bằng hàm

    feof [ resource $stream ] : bool

    Code language: PHP [php]
    2.
  • Đóng tệp bằng hàm

    feof [ resource $stream ] : bool

    Code language: PHP [php]
    3.

Tại đây, cú pháp của hàm

feof [ resource $stream ] : bool

Code language: PHP [php]
2:

fread [ resource $stream , int $length ] : string|false

Code language: PHP [php]

Hàm

feof [ resource $stream ] : bool

Code language: PHP [php]
2 có hai tham số:

  • feof [ resource $stream ] : bool

    Code language: PHP [php]
    6 là tài nguyên con trỏ hệ thống tệp, thường là kết quả của hàm

    feof [ resource $stream ] : bool

    Code language: PHP [php]
    1.
  • feof [ resource $stream ] : bool

    Code language: PHP [php]
    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ố

    feof [ resource $stream ] : bool

    Code language: PHP [php]
    8.

Hàm

feof [ resource $stream ] : bool

Code language: PHP [php]
2 trả về nội dung tệp hoặc

fgets [ resource $handle , int $length = ? ] : string|false

Code language: PHP [php]
1 nếu không đọc.

Hàm

feof [ resource $stream ] : bool

Code language: PHP [php]
2 ngừng đọc tệp sau khi số lượng byte

feof [ resource $stream ] : bool

Code language: PHP [php]
8 đã đượ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

fgets [ resource $handle , int $length = ? ] : string|false

Code language: PHP [php]
4:

feof [ resource $stream ] : bool

Code language: PHP [php]

Hàm

fgets [ resource $handle , int $length = ? ] : string|false

Code language: PHP [php]
4 trả về

fgets [ resource $handle , int $length = ? ] : string|false

Code language: PHP [php]
6 nếu

feof [ resource $stream ] : bool

Code language: PHP [php]
6 ở EOF hoặc xảy ra lỗi. Nếu không, nó trả về

fgets [ resource $handle , int $length = ? ] : string|false

Code language: PHP [php]
1.

Để đọc một dòng tệp từng dòng, bạn sử dụng hàm

fgets [ resource $handle , int $length = ? ] : string|false

Code language: PHP [php]
9:

fgets [ resource $handle , int $length = ? ] : string|false

Code language: PHP [php]

Giống như hàm

feof [ resource $stream ] : bool

Code language: PHP [php]
2, hàm

fgets [ resource $handle , int $length = ? ] : string|false

Code language: PHP [php]
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ố

feof [ resource $stream ] : bool

Code language: PHP [php]
8, hàm

feof [ resource $stream ] : bool

Code language: PHP [php]
2 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

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]
4 được đặt tại thư mục

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]
5 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

feof [ resource $stream ] : bool

Code language: PHP [php]
2 để đọc nội dung của toàn bộ tệp

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]
4 thành một chuỗi và hiển thị nó trên trang web:

Bài Viết Liên Quan

Chủ Đề