Hi Folks, after seeing the new function file_get_contents() I figured a corresponding file_put() function should be added too.
Prototype: int file_put(string filename, mixed data [, string mode]) It takes a filename (URL wrappers supported), the data to be written and a mode for the file. The data can be either a string or an array (which is like using join("", $data) first and can be used to write data read by the file() function). The mode can be "a" or "ab" to append to a file and defaults to "wb". The function returns the number of bytes written to the stream. A test for the function has been included in ext/standard/tests/file/002.phpt. On a side note: I think the file_get_contents() function should be renamed to file_get() as it is useful enough to deserve a shorter name. Any comments on this function (and the implementation) are welcome. - Chris
? ext/standard/file_put.c Index: ext/standard/basic_functions.c =================================================================== RCS file: /repository/php4/ext/standard/basic_functions.c,v retrieving revision 1.528 diff -u -r1.528 basic_functions.c --- ext/standard/basic_functions.c 6 Oct 2002 17:04:10 -0000 1.528 +++ ext/standard/basic_functions.c 16 Oct 2002 00:22:26 -0000 @@ -625,6 +625,7 @@ PHP_STATIC_FE("tmpfile", php_if_tmpfile, NULL) PHP_FE(file, NULL) PHP_FE(file_get_contents, NULL) + PHP_FE(file_put, + NULL) PHP_FE(stream_select, first_through_third_args_force_ref) PHP_FE(stream_context_create, NULL) PHP_FE(stream_context_set_params, NULL) Index: ext/standard/file.c =================================================================== RCS file: /repository/php4/ext/standard/file.c,v retrieving revision 1.270 diff -u -r1.270 file.c --- ext/standard/file.c 15 Oct 2002 16:45:26 -0000 1.270 +++ ext/standard/file.c 16 Oct 2002 00:22:26 -0000 @@ -452,6 +452,79 @@ } /* }}} */ +/* {{{ proto int file_put(string filename, mixed data [, string mode]) + Write an entire string or array to a file and return length written */ +PHP_FUNCTION(file_put) +{ + int ret; + char *filename; + int filename_len; + char *data = NULL; + int data_len; + zval *arr = NULL; + char *mode = "wb"; + int mode_len; + HashPosition pos; + zval **tmp = NULL; + php_stream *stream; + + /* Parse arguments */ + if ((zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() +TSRMLS_CC, "ss|s", + &filename, +&filename_len, &data, &data_len, &mode, &mode_len) == FAILURE) && + (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa|s", + &filename, &filename_len, &arr, &mode, &mode_len) == +FAILURE)) { + return; + } + + stream = php_stream_open_wrapper(filename, mode, + ENFORCE_SAFE_MODE | REPORT_ERRORS, + NULL); + if (!stream) { + RETURN_FALSE; + } + + if (arr) { + zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos); + + if (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&tmp, +&pos) == SUCCESS) { + convert_to_string_ex(tmp); + data = Z_STRVAL_PP(tmp); + data_len = Z_STRLEN_PP(tmp); + } + } + + while (data) { + char *buffer = NULL; + + if (PG(magic_quotes_runtime)) { + buffer = estrndup(data, data_len); + php_stripslashes(buffer, &data_len TSRMLS_CC); + } + + ret = php_stream_write(stream, buffer ? buffer : data, data_len); + if (buffer) { + efree(buffer); + } + + data = NULL; + + if (arr) { + zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos); + + if (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void +**)&tmp, &pos) == SUCCESS) { + convert_to_string_ex(tmp); + data = Z_STRVAL_PP(tmp); + data_len = Z_STRLEN_PP(tmp); + } + } + } + + php_stream_close(stream); + + RETURN_LONG(ret); +} +/* }}} */ + /* {{{ proto array file(string filename [, bool use_include_path]) Read entire file into an array */ Index: ext/standard/file.h =================================================================== RCS file: /repository/php4/ext/standard/file.h,v retrieving revision 1.70 diff -u -r1.70 file.h --- ext/standard/file.h 28 Sep 2002 22:14:21 -0000 1.70 +++ ext/standard/file.h 16 Oct 2002 00:22:26 -0000 @@ -54,6 +54,7 @@ PHP_FUNCTION(copy); PHP_FUNCTION(file); PHP_FUNCTION(file_get_contents); +PHP_FUNCTION(file_put); PHP_FUNCTION(set_socket_blocking); /* deprecated */ PHP_FUNCTION(stream_set_blocking); PHP_FUNCTION(stream_select); Index: ext/standard/tests/file/002.phpt =================================================================== RCS file: /repository/php4/ext/standard/tests/file/002.phpt,v retrieving revision 1.2 diff -u -r1.2 002.phpt --- ext/standard/tests/file/002.phpt 11 Apr 2002 06:41:55 -0000 1.2 +++ ext/standard/tests/file/002.phpt 16 Oct 2002 00:22:26 -0000 @@ -29,6 +29,8 @@ fwrite($fp, $data); fclose($fp); +file_put($name, array("\n", "more blah"), "ab"); + //readfile($name); echo file_get_contents($name); @@ -52,3 +54,4 @@ blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah +more blah
-- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php