Hàm fread() sẽ đọc nội dung của file truyền vào. Lưu ý hàm sẽ dừng việc đọc khi:
$lenght byte.fread ( resource$handle, int$length) : string
$handle là đường dẫn tới file cần đọc nội dung.$length là độ dài tối đa hàm sẽ đọc.<?php // get contents of a file into a string $filename = "/usr/local/something.txt"; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); ?>
<?php $filename = "c:\\files\\somepic.gif"; $handle = fopen($filename, "rb"); $contents = fread($handle, filesize($filename)); fclose($handle); ?>
<?php
// For PHP 5 and up
$handle = fopen("http://www.example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?><?php
$handle = fopen("http://www.example.com/", "rb");
if (FALSE === $handle) {
exit("Failed to open stream to URL");
}
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?><?php
$fp = fopen($DownloadFile, 'rb');
while ( $cline = fgets($fp) )
{
print $cline;
}
fclose($fp);
?>Bằng cách nào đó, đó là “an toàn nhị phân” và cung cấp tệp được đọc. md5 bản gốc và tải về là như nhau.<?php
ob_end_clean();
ob_start();
header( 'Content-Type:' );
?>xem vị trí được đặt trong hàm bên dưới:<?php
function readfile_chunked( $filename, $retbytes = true ) {
$chunksize = 1 * (1024 * 1024); // how many bytes per chunk
$buffer = '';
$cnt = 0;
$handle = fopen( $filename, 'rb' );
if ( $handle === false ) {
return false;
}
ob_end_clean(); //added to fix ZIP file corruption
ob_start(); //added to fix ZIP file corruption
header( 'Content-Type:' ); //added to fix ZIP file corruption
while ( !feof( $handle ) ) {
$buffer = fread( $handle, $chunksize );
//$buffer = str_replace("","",$buffer);
echo $buffer;
ob_flush();
flush();
if ( $retbytes ) {
$cnt += strlen( $buffer );
}
}
$status = fclose( $handle );
if ( $retbytes && $status ) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
?>Các bạn có thể xem chi tiết hơn trên php.net.
Hi vọng với bài viết này, bạn đã hiểu rõ ứng dụng của hàm fread() trong PHP. Nếu bạn thấy bài viết hay và có ý nghĩa hãy like và chia sẻ bài viết này để mọi người cùng nhau học tập nhé. Cảm ơn các bạn đã ghé thăm codetutam.com
Bình luận: