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

FGETCSV(3)								 1								FGETCSV(3)

fgetcsv - Gets line from file pointer and parse for CSV fields

SYNOPSIS
array fgetcsv (resource $handle, [int $length], [string $delimiter = ","], [string $enclosure = '"'], [string $escape = " DESCRIPTION
Similar to fgets(3) except that fgetcsv(3) parses the line it reads for fields in CSV format and returns an array containing the fields read. PARAMETERS
o $handle - A valid file pointer to a file successfully opened by fopen(3), popen(3), or fsockopen(3). o $length - Must be greater than the longest line (in characters) to be found in the CSV file (allowing for trailing line-end characters). It became optional in PHP 5. Omitting this parameter (or setting it to 0 in PHP 5.1.0 and later) the maximum line length is not limited, which is slightly slower. o $delimiter - The optional $delimiter parameter sets the field delimiter (one character only). o $enclosure - The optional $enclosure parameter sets the field enclosure character (one character only). o $escape - The optional $escape parameter sets the escape character (one character only). RETURN VALUES
Returns an indexed array containing the fields read. Note A blank line in a CSV file will be returned as an array comprising a single null field, and will not be treated as an error. Note If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem. fgetcsv(3) returns NULL if an invalid $handle is supplied or FALSE on other errors, including end of file. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.3.0 | | | | | | | The $escape parameter was added | | | | | 5.1.0 | | | | | | | The $length is now optional. Default is 0, mean- | | | ing no length limit. | | | | | 4.3.5 | | | | | | | fgetcsv(3) is now binary safe | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 Read and print the entire contents of a CSV file <?php $row = 1; if (($handle = fopen("test.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p> "; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . "<br /> "; } } fclose($handle); } ?> NOTES
Note Locale setting is taken into account by this function. If $LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function. SEE ALSO
str_getcsv(3), explode(3), file(3), pack(3), fputcsv(3). PHP Documentation Group FGETCSV(3)
Man Page