Introduction to PHP fread function
string fread (int handle, int length)
Fread () reads up to length bytes from the file pointer handle. The function stops reading a file when it reads up to a maximum length of bytes, or when it reaches EOF, or (for a network stream) when a package is available, or (after opening a user-space stream), 8,192 bytes have been read.
Fread () instance:
<?php
$file = "Data.txt";
$fh = fopen ($file, "RT");
$userdata = Fread ($fh, FileSize ($file));
Fclose ($FH);
? >
Some points to be noted in PHP using Fread
1, Fread read Write a large file error resolution
When you use Fread to read a file, you are prompted for an error if you exceed the maximum memory usage value set in PHP.ini, and the following method resolves the problem of reading a large file:
?
Set_time_limit (0)//Set script execution time infinitely long
$flie = "Flexbuilder_linux_install_a5_112409.bin";//large file exceeds memory configuration in php.ini
$ Fp=fopen ($flie, "R");
$content = "";
$filename = "123.bin";//Save as a new file
$handle =fopen ($filename, "a"),//write open, point the file pointer to the end of the file. If the file does not exist, try to create a while
(!feof ($fp)) {//test file pointer to the location of the end of the file
$content =fread ($FP, 1024);
Fwrite ($handle, $content);
}
Fclose ($FP);
Fclose ($handle);
echo "Data successfully written to file";
? >
2, PHP fread () is how to identify the file encoding
<?php
$handler =fopen (' a.txt ', ' RB ')//Binary mode opens
$content =fread ($handler, 1024);
echo $content;
? >
Fread as a string return, that it is how to identify a.txt use of the encoding method, to ensure that not garbled it?
PHP7.0 the following version of the file operation does not recognize character encoding.
Just by byte data output, if the PHP source file and output HTML character encoding consistent with the correct display.
3, Fread read the file will always be more than one empty character
$fileSize = FileSize ($filePath);
$handle = fopen ($filePath, "RB");
while (!feof ($handle)) {
var_dump (fread ($handle, $fileSize));
Will output one more blank character
}
When we use the above code to read files, there are sometimes more than one empty character. This is because you are the Windows platform, the file is the text to open the stored content, the end will have a special byte identification file end, you use RB open nature can read the last special byte. Open it with R, Fgets read it.
Through this article hope to help everyone, thank you for your support to this site!