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|falseCode 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|falseCode 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|falseCode language: PHP (php)
4:


feof ( resource $stream ) : bool Code language: PHP (php)

Hàm


fgets ( resource $handle , int $length = ? ) : string|falseCode language: PHP (php)
4 trả về

fgets ( resource $handle , int $length = ? ) : string|falseCode 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|falseCode 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|falseCode language: PHP (php)
9:


fgets ( resource $handle , int $length = ? ) : string|falseCode 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|falseCode 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:


<?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


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 bằng hàm

feof ( resource $stream ) : bool Code language: PHP (php)
1:


$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


feof ( resource $stream ) : bool Code language: PHP (php)
2; Sử dụng chức năng

<?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)
1 để 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


<?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)
2 để chuyển đổi các ký tự mới thành thẻ

<?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)
3.


echo nl2br($contents);Code language: PHP (php)

Cuối cùng, đóng tệp bằng hàm


feof ( resource $stream ) : bool Code language: PHP (php)
3.

Lưu ý rằng chức năng


<?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)
5 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


feof ( resource $stream ) : bool Code language: PHP (php)
2 để đọc tối đa 100 byte từ 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:


<?php $filename = './public/population.txt'; $f = fopen($filename, 'r'); if ($f) { $contents = fread($f, 100); fclose($f); echo nl2br($contents); }Code language: HTML, XML (xml)

Output:


1 New York New York 8,253,213 2 Los Angeles California 3,970,219 3 Chicago Illinois 2,677,64Code language: plaintext (plaintext)

3) Đọc từng dòng tệp

Ví dụ sau sử dụng hàm


fgets ( resource $handle , int $length = ? ) : string|falseCode language: PHP (php)
9 để đọc dòng 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 theo từng dòng:


feof ( resource $stream ) : bool Code language: PHP (php)
0

Bản tóm tắt

  • Sử dụng chức năng

    feof ( resource $stream ) : bool Code language: PHP (php)
    2 để đọc một số hoặc tất cả nội dung từ một tệp.
  • Sử dụng chức năng

    fgets ( resource $handle , int $length = ? ) : string|falseCode language: PHP (php)
    9 để đọc một dòng từ một tệp.
  • Sử dụng chức năng

    fgets ( resource $handle , int $length = ? ) : string|falseCode language: PHP (php)
    4 để kiểm tra phần cuối đã đạt được.
  • Sử dụng chức năng

    <?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)
    1 để 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?

Có bao nhiêu cách chúng ta có thể đọc tệp PHP?

Các phương thức để có được nội dung tệp: fgets (): fgets () Phương thức Đọc từng dòng tệp.Chúng tôi sử dụng lệnh FOEF để phát hiện phần cuối của tệp và đọc từng dòng tệp.Phương thức fread (): fread () được sử dụng khi chúng tôi muốn đọc tệp với kích thước giới hạn.fgets() : fgets() method read the file line by line. we use feof command to detect the end of the file and read the file line by line. fread() : fread() method is used when we want to read the file in limited size.

Làm thế nào chúng ta có thể đọc một tệp trong PHP giải thích với mã mẫu?

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ự ...
$ fp = fopen ("C: \\ file1.txt", "r"); // Mở tệp ở chế độ đọc ..
while (! feof ($ fp)) {.
echo fgetc ($ fp) ;.
fclose($fp);.

Đọc gì trong PHP?

Hàm readFile () đọc một tệp và ghi nó vào bộ đệm đầu ra.Mẹo: Bạn có thể sử dụng URL làm tên tệp với chức năng này nếu trình bao bọc fopen đã được bật trong PHP.Tệp INI.reads a file and writes it to the output buffer. Tip: You can use a URL as a filename with this function if the fopen wrappers have been enabled in the php. ini file.

Chức năng PHP nào được sử dụng để mở tệp?

Summary.

Tải thêm tài liệu liên quan đến bài viết 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?