Re: cvs: php4 /ext/standard basic_functions.c basic_functions.h
| From: | Rasmus Lerdorf | Date: | Sat, 23 Mar 2002 14:58:58 +0000 |
| Subject: | Re: cvs: php4 /ext/standard basic_functions.c basic_functions.h | ||
| References: | 1 | Groups: | php.cvs |
| Request: | Send a blank email to php-cvs+get-10611@lists.php.net to get a copy of this message | ||
Hrm.. currently readfile() doesn't return the file, but simply outputs it.
Whether that is right or wrong is certainly debatable, but for consistency
I think read_uploaded_file() should do the same and we should add a
different mechanism for simply returning the file. Perhaps an optional
2nd argument flag that tells the functions to return the contents instead.
On Sat, 23 Mar 2002, Derick Rethans wrote:
> derick Sat Mar 23 09:10:57 2002 EDT
>
> Modified files:
> /php4/ext/standard basic_functions.c basic_functions.h
> Log:
> - Added read_uploaded_file (patch by Andrew Sitnikov <sitnikov@infonet.ee>)
>
>
> Index: php4/ext/standard/basic_functions.c
> diff -u php4/ext/standard/basic_functions.c:1.464 php4/ext/standard/basic_functions.c:1.465
> --- php4/ext/standard/basic_functions.c:1.464 Thu Mar 21 20:10:31 2002
> +++ php4/ext/standard/basic_functions.c Sat Mar 23 09:10:57 2002
> @@ -17,7 +17,7 @@
> +----------------------------------------------------------------------+
> */
>
> -/* $Id: basic_functions.c,v 1.464 2002/03/22 01:10:31 hholzgra Exp $ */
> +/* $Id: basic_functions.c,v 1.465 2002/03/23 14:10:57 derick Exp $ */
>
> #include "php.h"
> #include "php_streams.h"
> @@ -527,6 +527,7 @@
> PHP_FE(parse_ini_file, NULL)
> PHP_FE(is_uploaded_file, NULL)
> PHP_FE(move_uploaded_file, NULL)
> + PHP_FE(read_uploaded_file, NULL)
>
> /* functions from type.c */
> PHP_FE(intval, NULL)
> @@ -2348,6 +2349,75 @@
> php_error(E_WARNING, "Unable to move '%s' to '%s'",
> Z_STRVAL_PP(path), Z_STRVAL_PP(new_path));
> }
> RETURN_BOOL(successful);
> +}
> +/* }}} */
> +
> +/* {{{ proto string read_uploaded_file(string path)
> + Read a file if and only if it was created by an upload and return it content or FALSE if
> error */
> +PHP_FUNCTION(read_uploaded_file)
> +{
> + zval **file;
> + FILE *fp;
> + int issock = 0, socketd = 0;
> + int fd;
> + struct stat sbuf;
> + size_t len, rlen;
> +
> + if (!SG(rfc1867_uploaded_files)) {
> + RETURN_FALSE;
> + }
> +
> + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &file) != SUCCESS) {
> + ZEND_WRONG_PARAM_COUNT();
> + }
> + convert_to_string_ex(file);
> +
> + if (!zend_hash_exists(SG(rfc1867_uploaded_files), Z_STRVAL_PP(file), Z_STRLEN_PP(file) + 1))
> {
> + php_error(E_WARNING, "'%s' is not uploaded file", Z_STRVAL_PP(file));
> + RETURN_FALSE;
> + }
> +
> + fp = php_fopen_wrapper(Z_STRVAL_PP(file), "rb", 0, &issock, &socketd, NULL
> TSRMLS_CC);
> +
> + if (!fp) {
> + php_error(E_WARNING, "%s(): Can not open file '%s'",
> get_active_function_name(TSRMLS_C), Z_STRVAL_PP(file));
> + RETURN_FALSE;
> + }
> +
> + fd = fileno(fp);
> +
> + if (fstat(fd, &sbuf) != 0) {
> + php_error(E_WARNING, "%s(): fstat failed", get_active_function_name(TSRMLS_C));
> + RETURN_FALSE;
> + }
> +
> + len = sbuf.st_size;
> +
> + Z_STRVAL_P(return_value) = emalloc(len + 1);
> +
> + if (!Z_STRVAL_P(return_value)) {
> + php_error(E_WARNING, "%s(): Cannot allocate %i bytes",
> get_active_function_name(TSRMLS_C), len);
> + RETURN_FALSE;
> + }
> +
> + rlen = fread(Z_STRVAL_P(return_value), 1, len, fp);
> +
> + fclose(fp);
> +
> + if (rlen != len) {
> + php_error(E_WARNING, "%s(): Can not read '%s'",
> get_active_function_name(TSRMLS_C), Z_STRVAL_PP(file));
> + efree(Z_STRVAL_P(return_value));
> + RETURN_FALSE;
> + }
> +
> + Z_STRLEN_P(return_value) = rlen;
> + Z_STRVAL_P(return_value)[Z_STRLEN_P(return_value)] = 0;
> +
> + if (PG(magic_quotes_runtime)) {
> + Z_STRVAL_P(return_value) = php_addslashes(Z_STRVAL_P(return_value),
> + Z_STRLEN_P(return_value), &Z_STRLEN_P(return_value), 1 TSRMLS_CC);
> + }
> + Z_TYPE_P(return_value) = IS_STRING;
> }
> /* }}} */
>
> Index: php4/ext/standard/basic_functions.h
> diff -u php4/ext/standard/basic_functions.h:1.103 php4/ext/standard/basic_functions.h:1.104
> --- php4/ext/standard/basic_functions.h:1.103 Wed Feb 20 22:32:42 2002
> +++ php4/ext/standard/basic_functions.h Sat Mar 23 09:10:57 2002
> @@ -17,7 +17,7 @@
> +----------------------------------------------------------------------+
> */
>
> -/* $Id: basic_functions.h,v 1.103 2002/02/21 03:32:42 andrei Exp $ */
> +/* $Id: basic_functions.h,v 1.104 2002/03/23 14:10:57 derick Exp $ */
>
> #ifndef BASIC_FUNCTIONS_H
> #define BASIC_FUNCTIONS_H
> @@ -95,6 +95,7 @@
>
> PHP_FUNCTION(is_uploaded_file);
> PHP_FUNCTION(move_uploaded_file);
> +PHP_FUNCTION(read_uploaded_file);
>
> /* From the INI parser */
> PHP_FUNCTION(parse_ini_file);
>
>
>
> --
> PHP CVS Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>