cvs: php4 /ext/standard basic_functions.c file.c file.h /main SAPI.c

From: Date: Fri, 08 Sep 2000 22:31:21 +0000
Subject: cvs: php4 /ext/standard basic_functions.c file.c file.h /main SAPI.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-1645@lists.php.net to get a copy of this message
zeev Fri Sep 8 15:31:21 2000 EDT Modified files: /php4/ext/standard basic_functions.c file.c file.h /php4/main SAPI.c Log: Implement move_uploaded_file() (untested) Index: php4/ext/standard/basic_functions.c diff -u php4/ext/standard/basic_functions.c:1.251 php4/ext/standard/basic_functions.c:1.252 --- php4/ext/standard/basic_functions.c:1.251 Fri Sep 8 14:58:28 2000 +++ php4/ext/standard/basic_functions.c Fri Sep 8 15:31:21 2000 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: basic_functions.c,v 1.251 2000/09/08 21:58:28 andrei Exp $ */ +/* $Id: basic_functions.c,v 1.252 2000/09/08 22:31:21 zeev Exp $ */ #include "php.h" #include "php_main.h" @@ -2227,6 +2227,41 @@ RETURN_FALSE; } } + + +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: Index: php4/ext/standard/file.c diff -u php4/ext/standard/file.c:1.108 php4/ext/standard/file.c:1.109 --- php4/ext/standard/file.c:1.108 Mon Aug 21 12:24:44 2000 +++ php4/ext/standard/file.c Fri Sep 8 15:31:21 2000 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.c,v 1.108 2000/08/21 19:24:44 torben Exp $ */ +/* $Id: file.c,v 1.109 2000/09/08 22:31:21 zeev Exp $ */ /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */ @@ -1731,8 +1731,6 @@ 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 @@ 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 */ Index: php4/ext/standard/file.h diff -u php4/ext/standard/file.h:1.25 php4/ext/standard/file.h:1.26 --- php4/ext/standard/file.h:1.25 Sun Jul 23 18:39:49 2000 +++ php4/ext/standard/file.h Fri Sep 8 15:31:21 2000 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.h,v 1.25 2000/07/24 01:39:49 david Exp $ */ +/* $Id: file.h,v 1.26 2000/09/08 22:31:21 zeev Exp $ */ /* Synced with php 3.0 revision 1.30 1999-06-16 [ssb] */ @@ -71,5 +71,6 @@ 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 */ Index: php4/main/SAPI.c diff -u php4/main/SAPI.c:1.89 php4/main/SAPI.c:1.90 --- php4/main/SAPI.c:1.89 Fri Sep 8 14:56:47 2000 +++ php4/main/SAPI.c Fri Sep 8 15:31:21 2000 @@ -247,7 +247,7 @@ 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);

« previous php.cvs (#1645) next »