php fread() Function is used to read files (can be safely used for binary files). This article introduces to you an example of freadreading a file and several things you need to pay attention to when using this function to read files. Friends in need can refer to it.
Introduction to php fread function
Syntax
fread(file,length)
Parameter
file Required. Specifies that the file should be opened for reading.
length Required. Specifies the maximum number of bytes to read.
Description
fread() reads up to length bytes from the file pointer file. This function is called when it has read up to length bytes, or when EOF is reached, or (for network streams) when a packet is available, or (after opening a userspace stream) 8192 bytes have been read. Will stop reading the file, depending on which condition is encountered first.
Returns the string read, or returns false if an error occurs.
fread() example:
Read 10 bytes from the file:
<?php $file = fopen("test.txt","r"); fread($file,"10"); fclose($file); ?>
Some points you need to pay attention to when using fread in php
1. Solution to error when fread reads and writes large files
When using fread to read files, if the maximum memory usage value set in php.ini is exceeded, a prompt will be displayed Error, the following method solves the problem of reading large files:
<? set_time_limit(0);//设置脚本执行时间无限长 $flie="flexbuilder_linux_install_a5_112409.bin";//大文件超过php.ini中的内存配置 $fp=fopen($flie,"r"); $content=""; $filename="123.bin";//存为新文件 $handle=fopen($filename,"a");//写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之 while(!feof($fp)){//测试文件指针是否到了文件结束的位置 $content=fread($fp,1024); fwrite($handle,$content); } fclose($fp); fclose($handle); echo "数据成功写入文件"; ?>
2. How does php fread() identify the file encoding
<?php $handler=fopen('a.txt','rb')//二进制方式打开 $content=fread($handler,1024); echo $content; ?>
fread uses The string is returned in the form of , so how does it identify the encoding method used in a.txt to ensure that it is not garbled?
Character encoding is not recognized in file operations of versions below PHP7.0.
Only outputs data in bytes. If it is consistent with the character encoding of the PHP source code file and the output HTML, it can be displayed correctly.
3. When fread reads a file, there will always be an extra null character.
$fileSize = filesize($filePath); $handle = fopen($filePath, "rb"); while (!feof($handle)) { var_dump(fread($handle, $fileSize)); //会多输出一次空字符 }
When we use the above code to read a file, sometimes there will be an extra null character. This is because you are on a WINDOWS platform, and the file is a text file that is opened and stored. There will be a special byte at the end to mark the end of the file. If you open it with rb, you can naturally read the last special byte. Open it with r and read it with fgets.
The above is the detailed content of What should you pay attention to when reading files with php fread?. For more information, please follow other related articles on the PHP Chinese website!