Hướng dẫn how do we read a file in php? - làm thế nào để chúng tôi đọc một tệp trong php?

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 rathis 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 rathis 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ục

feof ( 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
0

Output:

this is first line this is another line this is third line
1

3) Đọ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ệp

feof ( 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
2

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

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 cách nào để mở một tệp đọc trong PHP?

Hàm php fopen () được sử dụng để mở tệp hoặc url và trả về tài nguyên.Hàm fopen () chấp nhận hai đối số: $ fileName và $ Mode.Tên tệp $ đại diện cho tệp được mở và $ Mode đại diện cho chế độ tệp, ví dụ chỉ là đọc, đọc-viết, chỉ ghi, v.v. is used to open file or URL and returns resource. The fopen() function accepts two arguments: $filename and $mode. The $filename represents the file to be opended and $mode represents the file mode for example read-only, read-write, write-only etc.

Làm cách nào để đọc tệp PHP trong HTML?

Nhấp vào nút Mở trong trình duyệt trên thanh trạng thái ..
Trong trình chỉnh sửa, nhấp chuột phải vào tệp và nhấp vào menu ngữ cảnh Mở PHP/HTML/JS trong trình duyệt ..
Sử dụng KeyBindings Shift + F6 để mở nhanh hơn (có thể được thay đổi trong tệp menu -> Tùy chọn -> Phím tắt).

Chức năng đọc tệp PHP nào?

Hàm readFile () trong PHP là một hàm sẵn được sử dụng để đọc một tệp và ghi nó vào bộ đệm đầu ra.Tên tệp được gửi dưới dạng tham số cho hàm readfile () và nó trả về số byte đọc trên thành công, hoặc sai và lỗi về lỗi.readfile() function in PHP is an inbuilt function which is used to read a file and write it to the output buffer. The filename is sent as a parameter to the readfile() function and it returns the number of bytes read on success, or FALSE and an error on failure.