Home > Backend Development > PHP Tutorial > What should you pay attention to when reading files with php fread?

What should you pay attention to when reading files with php fread?

怪我咯
Release: 2023-03-13 21:22:01
Original
2199 people have browsed it

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)
Copy after login

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);
?>
Copy after login

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 "数据成功写入文件"; 
?>
Copy after login

2. How does php fread() identify the file encoding

<?php
$handler=fopen(&#39;a.txt&#39;,&#39;rb&#39;)//二进制方式打开
$content=fread($handler,1024);
echo $content;
?>
Copy after login

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));
    //会多输出一次空字符
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template