fread
READS the contents file pointed by a handle.
This is a binary-safe function.
A binary-safe function is one that treats its input as a raw stream of bytes and ignores every textual aspect it may have.
The term is mainly used in the PHP programming language to describe expected behaviour when passing binary data into functions whose main responsibility is text and string manipulating, and is used widely in the official PHP documentation.
Returns the read STRING or FALSE on failure.
Reading stops as soon as one of the following conditions is met:
$length bytes have been read.
The EOF, (end of file), is reached.
A packet becomes available or the socket timeout occurs, (for network streams).
If the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size, (usually 8192), is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.
<?php
str|false fread ( res $stream , int $length )
where,
$stream = A file system pointer
$lenght = The number of bytes to be read
?>
$stream
The file system pointer.
$length
Up to the number of bytes read.
EXERCISE
<?php
$fp = fopen( __DIR__ . '/temp/blahblahblah.txt', 'rb');
$cont01 = fread($fp, 8192);
echo $cont01;
fclose($fp);
?>
EXERCISE
<?php
$fp = fopen('file://' . __DIR__ . '/adds/love.txt', 'rb');
$len02 = 166;
// NUMBER OF CHARACTERS TO BE READ
$cont02 = fread($fp, $len02);
echo $cont02 . '<br><br>' . $len02 . ' characters read';
fclose($fp);
?>
EXERCISE
<?php
$dir = __FILE__;
$s = fopen($dir, "r");
echo __FILE__ . '<br><br>';
$ss = fread($s, 8192);
echo $ss;
fclose($s);
?>
EXERCISE
<?php
define('PATHW', 'file://' . __DIR__ . '/temp/');
$file04 = 'Your Wildest Dreams.m4a';
$fp = fopen(PATHW . $file04, 'rb');
$len04 = filesize(PATHW . $file04);
$cont04 = fread($fp, $len04);
echo $cont04 . '<br><br><br>' . $len04 . ' characters read';
fclose($fp);
?>