fread
fread
Binary-safe file read
**fread** resource $stream int $length
reads up to
bytes from the file pointer
referenced by . Reading stops as soon as one of the following conditions is met:
fread``length``stream- bytes have been read length
-
EOF (end of file) is reached
-
a packet becomes available or the occurs (for network streams) socket timeout
-
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.
stream``lengthUp to number of bytes read.length
Returns the read string return.falseforfailure.
Voorbeeld: A simple 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);
?>
Voorbeeld: Binary example
Waarschuwing: > On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in mode parameter.
fopen
<?php
$filename = "c:\\files\\somepic.gif";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
Voorbeeld: Remote examples
Waarschuwing: > When reading from anything that is not a regular local file, such as streams returned when reading or from and , 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. remote files
popen``fsockopen
<?php
$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);
?>
Opmerking: > If you just want to get the contents of a file into a string, use as it has much better performance than the code above.
file_get_contents
Opmerking: > Note that reads from the current position of the file pointer. Use to find the current position of the pointer and to rewind the pointer position.
fread``ftell``rewind
fwrite``fopen``fsockopen``popen``fgets``fgetss``fscanf``file``fpassthru``fseek``ftell``rewind``unpack