diff options
author | Wez Furlong <wez@php.net> | 2002-03-16 16:06:18 +0000 |
---|---|---|
committer | Wez Furlong <wez@php.net> | 2002-03-16 16:06:18 +0000 |
commit | 044732711842a61ba555cc0782a85fb1e7097dd1 (patch) | |
tree | ae874b2d87bd5631a4492af7ff777547262ec743 /ext/standard/file.c | |
parent | ac50b53018fe62961313d07fec3aaea7c898e2e6 (diff) | |
download | php-git-044732711842a61ba555cc0782a85fb1e7097dd1.tar.gz |
Implement get_file_contents() as discussed (briefly!) by myself, Derick
and Sterling on php-dev some months ago.
It returns the file contents as a string, and uses mmap if possible.
Diffstat (limited to 'ext/standard/file.c')
-rw-r--r-- | ext/standard/file.c | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/ext/standard/file.c b/ext/standard/file.c index 5734db3a1f..06bcb9daf1 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -365,6 +365,49 @@ PHP_FUNCTION(get_meta_tags) } /* }}} */ + +/* {{{ proto string get_file_contents(string filename [, bool use_include_path]) + Read the entire file into a string */ +PHP_FUNCTION(get_file_contents) +{ + char *filename; + int filename_len; + char *contents, *target_buf; + zend_bool use_include_path = 0; + php_stream *stream; + int len, newlen; + + /* Parse arguments */ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", + &filename, &filename_len, &use_include_path) == FAILURE) { + return; + } + + stream = php_stream_open_wrapper(filename, "rb", + use_include_path | ENFORCE_SAFE_MODE | REPORT_ERRORS, + NULL TSRMLS_CC); + if (!stream) { + RETURN_FALSE; + } + + /* uses mmap if possible */ + if ((len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0)) > 0) { + + if (PG(magic_quotes_runtime)) { + contents = php_addslashes(contents, len, &newlen, 1 TSRMLS_CC); /* 1 = free source string */ + len = newlen; + } + + RETVAL_STRINGL(contents, len); + } else { + RETVAL_FALSE; + } + + php_stream_close(stream); + +} +/* }}} */ + /* {{{ proto array file(string filename [, bool use_include_path]) Read entire file into an array */ @@ -390,7 +433,7 @@ PHP_FUNCTION(file) stream = php_stream_open_wrapper(filename, "rb", use_include_path | ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL TSRMLS_CC); - if (!stream) { + if (!stream) { RETURN_FALSE; } |