网站地图    收藏   

主页 > 后端 > PHP语言 >

php file_get_contents 读取大文件出错_自学php网

来源:自学PHP网    时间:2014-12-04 21:50 作者: 阅读:

[导读] Note: string can be as large as 2GB. file_get_contents如果正常返回,会把文件内容储存到某个字符串中,所以它不应该返回超过2G长度的字符串。 如果文件内容超过2G,不加offset和maxlen调用file_get...

Note: string can be as large as 2GB.
file_get_contents如果正常返回,会把文件内容储存到某个字符串中,所以它不应该返回超过2G长度的字符串。
如果文件内容超过2G,不加offset和maxlen调用file_get_contents的话,肯定会返回false,
在64位机上面啥错都不报啊,我晕:


PHP 5.4.16 (cli) (built: Jun 7 2013 14:32:19)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with XCache v3.0.2, Copyright (c) 2005-2013, by mOo
with XCache Optimizer v3.0.2, Copyright (c) 2005-2013, by mOo
with XCache Cacher v3.0.2, Copyright (c) 2005-2013, by mOo
with XCache Coverager v3.0.2, Copyright (c) 2005-2013, by mOo
 

回到32位机,报这样的错:
PHP 5.3.10 (cli) (built: Jul 7 2012 12:30:39)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies

Warning: file_get_contents(./2g): failed to open stream: File too large in /data/app/zhaokunyao/test.php on line 4


/* {{{ proto string file_get_contents(string filename [, bool use_include_path [, resource context [, long offset [, long maxlen]]]])
Read the entire file into a string */
PHP_FUNCTION(file_get_contents)
{
char *filename;
int filename_len;
char *contents;
zend_bool use_include_path = 0;
php_stream *stream;
#int 的最大值是 2^31 -1 = 2147483648 -1 = 2G -1
int len;
long offset = -1;
long maxlen = PHP_STREAM_COPY_ALL;
zval *zcontext = NULL;
php_stream_context *context = NULL;

/* Parse arguments */
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|br!ll", &filename, &filename_len, &use_include_path, &zcontext, &offset, &maxlen) == FAILURE) {
return;
}

if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "length must be greater than or equal to zero");
RETURN_FALSE;
}

context = php_stream_context_from_zval(zcontext, 0);

stream = php_stream_open_wrapper_ex(filename, "rb",
(use_include_path ? USE_PATH : 0) | REPORT_ERRORS,
NULL, context);
if (!stream) {
RETURN_FALSE;
}

if (offset > 0 && php_stream_seek(stream, offset, SEEK_SET) < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to seek to position %ld in the stream", offset);
php_stream_close(stream);
RETURN_FALSE;
}

#php_stream_copy_to_mem的返回值是 size_t , 32位机上是 unsigned int , 64位机上是 long unsigned int.
#如果php_stream_copy_to_mem的返回值大于2G,那么len会溢出成为负数 <0,所以会返回FALSE.
#其实也有可能溢出成为正数的,比如4294968320...就会变成1024。。于是file_get_contents() 只返回前1024字节。

if ((len = php_stream_copy_to_mem(stream, &contents, maxlen, 0)) > 0) {
RETVAL_STRINGL(contents, len, 0);
} else if (len == 0) {
RETVAL_EMPTY_STRING();
} else {
RETVAL_FALSE;
}

php_stream_close(stream);
}

 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论