mirror of
https://github.com/php/php-src.git
synced 2026-03-28 10:12:18 +01:00
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.
This commit is contained in:
@@ -611,6 +611,7 @@ function_entry basic_functions[] = {
|
||||
PHP_FE(tempnam, NULL)
|
||||
PHP_STATIC_FE("tmpfile", php_if_tmpfile, NULL)
|
||||
PHP_FE(file, NULL)
|
||||
PHP_FE(get_file_contents, NULL)
|
||||
PHP_FE(fgetcsv, NULL)
|
||||
PHP_FE(flock, NULL)
|
||||
PHP_FE(get_meta_tags, NULL)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +90,7 @@ PHPAPI int php_stream_free(php_stream *stream, int call_dtor) /* {{{ */
|
||||
ret = stream->ops->close(stream, call_dtor);
|
||||
stream->abstract = NULL;
|
||||
|
||||
if (call_dtor) {
|
||||
if (call_dtor) {
|
||||
/* tidy up any FILE* that might have been fdopened */
|
||||
if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FDOPEN && stream->stdiocast) {
|
||||
fclose(stream->stdiocast);
|
||||
|
||||
Reference in New Issue
Block a user