From: Zeev Suraski Date: Fri, 8 Sep 2000 22:31:21 +0000 (+0000) Subject: Implement move_uploaded_file() (untested) X-Git-Tag: php-4.0.3RC1~236 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3edf46ff735f1f9d51979c31097335d819e71f7a;p=php Implement move_uploaded_file() (untested) --- diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 973fc3d3e4..7add16113f 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -2228,6 +2228,41 @@ PHP_FUNCTION(is_uploaded_file) } } + +PHP_FUNCTION(move_uploaded_file) +{ + zval **path, **new_path; + SLS_FETCH(); + + if (!SG(rfc1867_uploaded_files)) { + RETURN_FALSE; + } + + if (ZEND_NUM_ARGS()!=2 || zend_get_parameters_ex(2, &path, &new_path)!=SUCCESS) { + ZEND_WRONG_PARAM_COUNT(); + } + convert_to_string_ex(path); + convert_to_string_ex(new_path); + + if (!zend_hash_exists(SG(rfc1867_uploaded_files), Z_STRVAL_PP(path), Z_STRLEN_PP(path)+1)) { + RETURN_FALSE; + } + + if (rename(Z_STRVAL_PP(path), Z_STRVAL_PP(new_path))==0) { + RETURN_TRUE; + } + + if (php_copy_file(Z_STRVAL_PP(path), Z_STRVAL_PP(new_path))==SUCCESS) { + unlink(Z_STRVAL_PP(path)); + RETURN_TRUE; + } + + php_error(E_WARNING, "Unable to move '%s' to '%s'", Z_STRVAL_PP(path), Z_STRVAL_PP(new_path)); + + RETURN_FALSE; +} + + /* * Local variables: * tab-width: 4 diff --git a/ext/standard/file.c b/ext/standard/file.c index 06932de961..e2df54c91e 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -1731,8 +1731,6 @@ PHP_FUNCTION(fstat) PHP_FUNCTION(copy) { pval **source, **target; - char buffer[8192]; - int fd_s,fd_t,read_bytes; PLS_FETCH(); if (ARG_COUNT(ht) != 2 || zend_get_parameters_ex(2, &source, &target) == FAILURE) { @@ -1746,40 +1744,56 @@ PHP_FUNCTION(copy) RETURN_FALSE; } + if (php_copy_file(Z_STRVAL_PP(source), Z_STRVAL_PP(target))==SUCCESS) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} + +/* }}} */ + + +PHPAPI int php_copy_file(char *src, char *dest) +{ + char buffer[8192]; + int fd_s,fd_t,read_bytes; + #ifdef PHP_WIN32 - if ((fd_s=V_OPEN((Z_STRVAL_PP(source),O_RDONLY|_O_BINARY)))==-1) { + if ((fd_s=V_OPEN((src,O_RDONLY|_O_BINARY)))==-1) { #else - if ((fd_s=V_OPEN((Z_STRVAL_PP(source),O_RDONLY)))==-1) { + if ((fd_s=V_OPEN((src,O_RDONLY)))==-1) { #endif - php_error(E_WARNING,"Unable to open '%s' for reading: %s", Z_STRVAL_PP(source), strerror(errno)); - RETURN_FALSE; + php_error(E_WARNING,"Unable to open '%s' for reading: %s", src, strerror(errno)); + return FAILURE; } #ifdef PHP_WIN32 - if ((fd_t=V_OPEN((Z_STRVAL_PP(target),_O_WRONLY|_O_CREAT|_O_TRUNC|_O_BINARY,_S_IREAD|_S_IWRITE)))==-1){ + if ((fd_t=V_OPEN((dest,_O_WRONLY|_O_CREAT|_O_TRUNC|_O_BINARY,_S_IREAD|_S_IWRITE)))==-1) { #else - if ((fd_t=V_CREAT(Z_STRVAL_PP(target),0777))==-1) { + if ((fd_t=V_CREAT((dest,0777))==-1) { #endif - php_error(E_WARNING,"Unable to create '%s': %s", Z_STRVAL_PP(target), strerror(errno)); + php_error(E_WARNING,"Unable to create '%s': %s", dest, strerror(errno)); close(fd_s); - RETURN_FALSE; + return FAILURE; } while ((read_bytes=read(fd_s,buffer,8192))!=-1 && read_bytes!=0) { if (write(fd_t,buffer,read_bytes)==-1) { - php_error(E_WARNING,"Unable to write to '%s': %s", Z_STRVAL_PP(target), strerror(errno)); + php_error(E_WARNING,"Unable to write to '%s': %s", dest, strerror(errno)); close(fd_s); close(fd_t); - RETURN_FALSE; + return FAILURE; } } close(fd_s); close(fd_t); + return SUCCESS; +} + + - RETVAL_TRUE; -} -/* }}} */ /* {{{ proto int fread(int fp, int length) Binary-safe file read */ diff --git a/ext/standard/file.h b/ext/standard/file.h index 636499e23c..0107631d1d 100644 --- a/ext/standard/file.h +++ b/ext/standard/file.h @@ -71,5 +71,6 @@ PHPAPI int php_file_le_fopen(void); PHPAPI int php_file_le_popen(void); PHPAPI int php_file_le_socket(void); PHPAPI int php_file_le_uploads(void); +PHPAPI int php_copy_file(char *src, char *dest); #endif /* FILE_H */ diff --git a/main/SAPI.c b/main/SAPI.c index cee1f77876..a24ca9aaf8 100644 --- a/main/SAPI.c +++ b/main/SAPI.c @@ -247,7 +247,7 @@ SAPI_API size_t sapi_apply_default_charset(char **mimetype, size_t len SLS_DC) if (*charset && strncmp(*mimetype, "text/", 5) == 0 && strstr(*mimetype, "charset=") == NULL) { newlen = len + (sizeof(";charset=")-1) + strlen(charset); newtype = emalloc(newlen + 1); - PHP_STRLCPY(newtype, *mimetype, newlen + 1, len); + PHP_STRLCPY(newtype, *mimetype, newlen + 1, len); strlcat(newtype, ";charset=", newlen + 1); if (*mimetype != NULL) { efree(*mimetype);