php读文件出错,php file_get_contents 读取大文件出错

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);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值