Hướng dẫn php read binary file - php đọc tệp nhị phân

(Php 4, Php 5, Php 7, Php 8)

FREAD-Tệp an toàn nhị phân đọcBinary-safe file read

Sự mô tả

fread (tài nguyên $stream, int $length): chuỗi | sai(resource $stream, int $length): string|false

  • length byte đã được đọc
  • EOF (kết thúc tệp) đã đạt được
  • Một gói có sẵn hoặc thời gian chờ ổ cắm xảy ra (đối với các luồng mạng)
  • Nếu luồng được đọc đệm và nó không đại diện cho một tệp đơn giản, nhiều nhất là một tệp đọc lên đến một số byte bằng với kích thước chunk (thường là 8192) được thực hiện; Tùy thuộc vào dữ liệu được đệm trước đó, kích thước của dữ liệu được trả về có thể lớn hơn kích thước chunk.

Thông số

stream

Một tài nguyên con trỏ hệ thống tệp thường được tạo bằng fopen ().resource that is typically created using fopen().

length

Lên đến length số byte đọc.

Trả về giá trị

Trả về chuỗi đọc hoặc false khi thất bại.false on failure.

Ví dụ

Ví dụ #1 ví dụ Fread () đơn giảnfread() example

// get contents of a file into a string
$filename "/usr/local/something.txt";
$handle fopen($filename"r");
$contents fread($handlefilesize($filename));
fclose($handle);
?>

Ví dụ #2 Ví dụ về nhị phân ()fread() example

Cảnh báo

Trên các hệ thống phân biệt giữa các tệp nhị phân và văn bản (tức là Windows), tệp phải được mở bằng 'B' được bao gồm trong tham số chế độ fopen ().fopen() mode parameter.

$filename "c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>

Ví dụ #3 ví dụ fread () từ xafread() examples

Cảnh báo

Trên các hệ thống phân biệt giữa các tệp nhị phân và văn bản (tức là Windows), tệp phải được mở bằng 'B' được bao gồm trong tham số chế độ fopen ().popen() and fsockopen(), reading will stop after a packet is available. This means that you should collect the data together in chunks as shown in the examples below.

$handle fopen("http://www.example.com/""rb");
$contents stream_get_contents($handle);
fclose($handle);
?>

$length0

$length1

$length2

Ví dụ #3 ví dụ fread () từ xa

Khi đọc từ bất cứ điều gì không phải là một tệp cục bộ thông thường, chẳng hạn như các luồng được trả về khi đọc các tệp từ xa hoặc từ popen () và fsockopen (), đọc sẽ dừng sau khi có một gói. Điều này có nghĩa là bạn nên thu thập dữ liệu với nhau trong các khối như trong các ví dụ dưới đây.:

Ghi chúfile_get_contents() as it has much better performance than the code above.

Khi đọc từ bất cứ điều gì không phải là một tệp cục bộ thông thường, chẳng hạn như các luồng được trả về khi đọc các tệp từ xa hoặc từ popen () và fsockopen (), đọc sẽ dừng sau khi có một gói. Điều này có nghĩa là bạn nên thu thập dữ liệu với nhau trong các khối như trong các ví dụ dưới đây.:

Ghi chúfread() reads from the current position of the file pointer. Use ftell() to find the current position of the pointer and rewind() to rewind the pointer position.

Ghi chú:

  • Nếu bạn chỉ muốn lấy nội dung của một tệp vào một chuỗi, hãy sử dụng file_get_contents () vì nó có hiệu suất tốt hơn nhiều so với mã trên.
  • Lưu ý rằng fread () đọc từ vị trí hiện tại của con trỏ tệp. Sử dụng ftell () để tìm vị trí hiện tại của con trỏ và tua lại () để tua lại vị trí con trỏ.
  • Xem thêm
  • fwrite () - Viết tệp an toàn nhị phân
  • fopen () - Mở tệp hoặc URL
  • fsockopen () - Mở Internet hoặc kết nối ổ cắm tên miền UNIX
  • popen () - Mở con trỏ tệp quy trình
  • fgets () - Nhận dòng từ con trỏ tệp
  • fgetss () - Nhận dòng từ con trỏ tệp và thẻ HTML dải
  • fscanf () - Đầu vào phân tích cú pháp từ một tệp theo định dạng
  • file () - đọc toàn bộ tệp vào một mảng
  • fpassthru () - đầu ra tất cả dữ liệu còn lại trên con trỏ tệp
  • FSEEK () - Tìm kiếm trên một con trỏ tệp

ftell () - Trả về vị trí hiện tại của con trỏ đọc/ghi tệp

tua lại () - Tua lại vị trí của con trỏ tệp

$length3

$length4

$length5

$length6

$length7

unpack () - Giải nén dữ liệu từ chuỗi nhị phân

Edward Jaramilla ¶

$length8

$length9

length0

length1

length2

length3

length4

14 năm trước

dharmilshah tại gmail dot com ¶

length5

length6

length7

length8

length4

8 năm trước

tua lại () - Tua lại vị trí của con trỏ tệp

stream0

stream1

stream2

stream3

unpack () - Giải nén dữ liệu từ chuỗi nhị phân

Edward Jaramilla ¶

stream4

stream5

stream6

stream7

stream8

14 năm trước

Edward Jaramilla ¶

stream9

length0

length1

length4

14 năm trước

dharmilshah tại gmail dot com ¶

length3

length4

length5

length4

8 năm trước

Edgarinvillegas tại hotmail dot com ¶

length7

length8

length4

13 năm trước

tua lại () - Tua lại vị trí của con trỏ tệp

length0

13 năm trước

tua lại () - Tua lại vị trí của con trỏ tệp

length1

length2

length4

unpack () - Giải nén dữ liệu từ chuỗi nhị phân

Edward Jaramilla ¶

length4

length5

length6

length7

14 năm trước

Edward Jaramilla ¶

length8

length9

false0

false1

length4

14 năm trước

Edward Jaramilla ¶

false3

false4

false5

false6

length4

14 năm trước

Edward Jaramilla ¶

false8

false9

// get contents of a file into a string
$filename "/usr/local/something.txt";
$handle fopen($filename"r");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
0

length4

14 năm trước

Edward Jaramilla ¶

// get contents of a file into a string
$filename "/usr/local/something.txt";
$handle fopen($filename"r");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
2

// get contents of a file into a string
$filename "/usr/local/something.txt";
$handle fopen($filename"r");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
3

length4

14 năm trước

tua lại () - Tua lại vị trí của con trỏ tệp

// get contents of a file into a string
$filename "/usr/local/something.txt";
$handle fopen($filename"r");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
5

// get contents of a file into a string
$filename "/usr/local/something.txt";
$handle fopen($filename"r");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
6

length4

13 năm trước

dharmilshah tại gmail dot com ¶

// get contents of a file into a string
$filename "/usr/local/something.txt";
$handle fopen($filename"r");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
8

// get contents of a file into a string
$filename "/usr/local/something.txt";
$handle fopen($filename"r");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
9

length4

8 năm trước

Edward Jaramilla ¶

$filename "c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
1

$filename "c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
2

$filename "c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
3

$filename "c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
4

$filename "c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
5

$filename "c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
6

$filename "c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
7

$filename "c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
8

$filename "c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>
9

$handle fopen("http://www.example.com/""rb");
$contents stream_get_contents($handle);
fclose($handle);
?>
0

length4

14 năm trước

Edward Jaramilla ¶

$handle fopen("http://www.example.com/""rb");
$contents stream_get_contents($handle);
fclose($handle);
?>
2

$handle fopen("http://www.example.com/""rb");
$contents stream_get_contents($handle);
fclose($handle);
?>
3

length4

14 năm trước

dharmilshah tại gmail dot com ¶

$handle fopen("http://www.example.com/""rb");
$contents stream_get_contents($handle);
fclose($handle);
?>
5

$handle fopen("http://www.example.com/""rb");
$contents stream_get_contents($handle);
fclose($handle);
?>
6

$handle fopen("http://www.example.com/""rb");
$contents stream_get_contents($handle);
fclose($handle);
?>
7

$handle fopen("http://www.example.com/""rb");
$contents stream_get_contents($handle);
fclose($handle);
?>
8

$handle fopen("http://www.example.com/""rb");
$contents stream_get_contents($handle);
fclose($handle);
?>
9

$length00

length4

8 năm trước

Edgarinvillegas tại hotmail dot com ¶

$length02

$length03

length4

13 năm trước

mail ở 3v1n0 chấm net ¶

$length05

$length06

$length07

$length08

Matt tại Matt-Darby Dot Com ¶

dharmilshah tại gmail dot com ¶

$length09

$length10

length4

8 năm trước

Edward Jaramilla ¶

$length12

$length13

length4

14 năm trước

Edgarinvillegas tại hotmail dot com ¶

$length15

$length16

$length17

length4

13 năm trước

mail ở 3v1n0 chấm net ¶

$length19

Matt tại Matt-Darby Dot Com ¶

15 năm trước

$length20

$length21

$length22

aubfre tại hotmail dot com

Edward Jaramilla ¶

$length23

$length24

$length25

Tom ¶

14 năm trước

$length26

$length27

$length28

$length29

length4

fpinho tại hotpop dot com

18 năm trước

$length31

$length32

$length33

length4

cướp tại lbox.org ¶

19 năm trước

$length35

$length36

$length37

$length38

$length39