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
/* {{{ 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 file_get_contents 读取大文件出错的详细内容...