From 51ffc0177676fdd2d2f1767b427f005fd54bbef9 Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Wed, 13 Oct 2004 23:26:29 +0000 Subject: [PATCH] Added optional offset parameter to file_get_contents(). --- NEWS | 3 ++- ext/standard/file.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 93b39c1d8d..3747e4cb04 100644 --- a/NEWS +++ b/NEWS @@ -1,7 +1,8 @@ PHP NEWS ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ?? ??? 2004, PHP 5.1.0 -- Added optional offset parameter to stream_get_contents(). (Ilia) +- Added optional offset parameter to stream_get_contents() and + file_get_contents(). (Ilia) - Improved performance of: . general execution/compilation. (Andi, Thies, Sterling, Dmitry, Marcus) . switch statement. (Dmitry) diff --git a/ext/standard/file.c b/ext/standard/file.c index da53e19682..8869a1966a 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -462,7 +462,7 @@ PHP_FUNCTION(get_meta_tags) /* }}} */ -/* {{{ proto string file_get_contents(string filename [, bool use_include_path [, resource context]]) +/* {{{ proto string file_get_contents(string filename [, bool use_include_path [, resource context [, long offset]]]) Read the entire file into a string */ PHP_FUNCTION(file_get_contents) { @@ -472,12 +472,13 @@ PHP_FUNCTION(file_get_contents) zend_bool use_include_path = 0; php_stream *stream; int len, newlen; + long offset = -1; zval *zcontext = NULL; php_stream_context *context = NULL; /* Parse arguments */ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|br!", - &filename, &filename_len, &use_include_path, &zcontext) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|br!l", + &filename, &filename_len, &use_include_path, &zcontext, &offset) == FAILURE) { return; } @@ -490,6 +491,11 @@ PHP_FUNCTION(file_get_contents) 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 %ld position in the stream.", offset); + RETURN_FALSE; + } + /* uses mmap if possible */ if ((len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0)) > 0) { -- 2.50.1