Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

fread(3) [php man page]

FREAD(3)								 1								  FREAD(3)

fread - Binary-safe file read

SYNOPSIS
string fread (resource $handle, int $length) DESCRIPTION
fread(3) reads up to $length bytes from the file pointer referenced by $handle. Reading stops as soon as one of the following conditions is met: o$length bytes have been read o EOF (end of file) is reached o a packet becomes available or the socket timeout occurs (for network streams) o 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. PARAMETERS
o $handle -A file system pointer resource that is typically created using fopen(3). o $length - Up to $length number of bytes read. RETURN VALUES
Returns the read string or FALSE on failure. EXAMPLES
Example #1 A simple fread(3) example <?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); ?> Example #2 Binary fread(3) example Warning On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen(3) mode parameter. <?php $filename = "c:\files\somepic.gif"; $handle = fopen($filename, "rb"); $contents = fread($handle, filesize($filename)); fclose($handle); ?> Example #3 Remote fread(3) examples Warning When reading from anything that is not a regular local file, such as streams returned when reading remote files or from popen(3) and fsockopen(3), 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. <?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); ?> NOTES
Note If you just want to get the contents of a file into a string, use file_get_contents(3) as it has much better performance than the code above. Note Note that fread(3) reads from the current position of the file pointer. Use ftell(3) to find the current position of the pointer and rewind(3) to rewind the pointer position. SEE ALSO
fwrite(3), fopen(3), fsockopen(3), popen(3), fgets(3), fgetss(3), fscanf(3), file(3), fpassthru(3), ftell(3), rewind(3). PHP Documentation Group FREAD(3)

Check Out this Related Man Page

FSEEK(3)								 1								  FSEEK(3)

fseek - Seeks on a file pointer

SYNOPSIS
int fseek (resource $handle, int $offset, [int $whence = SEEK_SET]) DESCRIPTION
Sets the file position indicator for the file referenced by $handle. The new position, measured in bytes from the beginning of the file, is obtained by adding $offset to the position specified by $whence. In general, it is allowed to seek past the end-of-file; if data is then written, reads in any unwritten region between the end-of-file and the sought position will yield bytes with value 0. However, certain streams may not support this behavior, especially when they have an underlying fixed size storage. PARAMETERS
o $handle -A file system pointer resource that is typically created using fopen(3). o $offset - The offset. To move to a position before the end-of-file, you need to pass a negative value in $offset and set $whence to SEEK_END. o $whence -$whence values are: o SEEK_SET - Set position equal to $offset bytes. o SEEK_CUR - Set position to current location plus $offset. o SEEK_END - Set position to end-of-file plus $offset. RETURN VALUES
Upon success, returns 0; otherwise, returns -1. EXAMPLES
Example #1 fseek(3) example <?php $fp = fopen('somefile.txt', 'r'); // read some data $data = fgets($fp, 4096); // move back to the beginning of the file // same as rewind($fp); fseek($fp, 0); ?> NOTES
Note If you have opened the file in append ( a or a+) mode, any data you write to the file will always be appended, regardless of the file position, and the result of calling fseek(3) will be undefined. Note Not all streams support seeking. For those that do not support seeking, forward seeking from the current position is accomplished by reading and discarding data; other forms of seeking will fail. SEE ALSO
ftell(3), rewind(3). PHP Documentation Group FSEEK(3)
Man Page