Filesystem Funcții
PHP Manual

fread

(PHP 4, PHP 5, PHP 7)

freadBinary-safe file read

Descrierea

string fread ( resource $handle , int $length )

fread() reads up to length bytes from the file pointer referenced by handle. Reading stops as soon as one of the following conditions is met:

Parametri

handle

O resursă de tip indicator în sistemul de fișiere care este în mod tipic creată utilizând fopen().

length

Up to length number of bytes read.

Valorile întoarse

Returns the read string sau FALSE în cazul eșecului.

Exemple

Example #1 A simple fread() example

<?php
// get contents of a file into a string
$filename "/usr/local/something.txt";
$handle fopen($filename"r");
$contents fread($handlefilesize($filename));
fclose($handle);
?>

Example #2 Binary fread() example

Avertizare

On systems which differentiate between binary and text files (i.e. Windows) the file must be opened with 'b' included in fopen() mode parameter.

<?php
$filename 
"c:\\files\\somepic.gif";
$handle fopen($filename"rb");
$contents fread($handlefilesize($filename));
fclose($handle);
?>

Example #3 Remote fread() examples

Avertizare

When reading from anything that is not a regular local file, such as streams returned when reading remote files or from popen() and fsockopen(), 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($handle8192);
}
fclose($handle);
?>

Note

Notă:

If you just want to get the contents of a file into a string, use file_get_contents() as it has much better performance than the code above.

Notă:

Note that fread() reads from the current position of the file pointer. Use ftell() to find the current position of the pointer and rewind() to rewind the pointer position.

A se vedea și


Filesystem Funcții
PHP Manual