Home >Backend Development >PHP Tutorial >Solve the problem of file_get_contents returning false when reading large files in PHP_PHP Tutorial

Solve the problem of file_get_contents returning false when reading large files in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:43:291658browse

The file_get_contents file is used to read and write files, but I found an error message when using file_get_contents to read large files. Note: string can be as large as 2GB. This cannot exceed 2G. Is there a way to solve it? Below I Come and see it together.

If I read a www.bKjia.c0m file

The code is as follows
 代码如下 复制代码

$u ='www.bKjia.c0m'; //此文件为100GB

$a =file_get_contents( $u );

Copy code

$u ='www.bKjia.c0m'; //This file is 100GB

$a =file_get_contents( $u );


Run Tips

Note: string can be as large as 2GB

It cannot be larger than 2GB. Let’s go to the official website to see this function reference

 代码如下 复制代码

$u ='www.bKjia.c0m'; //此文件为100GB

$a =file_get_contents( $u,100,1000 );

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )

I found one

file_get_contents() reads the file into a string. Contents of length maxlen will be read starting at the position specified by the offset parameter. On failure, file_get_contents() returns FALSE.

That’s it, let’s modify the program

$u ='www.bKjia.c0m'; //This file is 100GB
The code is as follows

Copy code
$a =file_get_contents( $u,100,1000 );

Reading successful Summary If file_get_contents returns normally, it will store the file content in a string, so it should not return a string exceeding 2G in length. If the file content exceeds 2G, calling file_get_contents without adding offset and maxlen will definitely return false,
http://www.bkjia.com/PHPjc/633183.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633183.htmlTechArticleThe file_get_contents file is used to read and write files, but I found an error message when reading large files using file_get_contents Note: string can be as large as 2GB, this cannot exceed 2G,...
Statement:
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