@@ -146,6 +146,8 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, char *path, ch
146
146
php_stream * reuseid = NULL ;
147
147
char * tpath , * ttpath , * hoststart = NULL ;
148
148
size_t file_size = 0 ;
149
+ zval * * tmpzval ;
150
+ int allow_overwrite = 0 ;
149
151
150
152
tmp_line [0 ] = '\0' ;
151
153
@@ -312,10 +314,25 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, char *path, ch
312
314
php_stream_notify_file_size (context , file_size , tmp_line , result );
313
315
}
314
316
} else {
315
- /* when writing file, it must NOT exist */
317
+ /* when writing file, it must NOT exist, unless a context option exists which allows it */
318
+ if (context && php_stream_context_get_option (context , "ftp" , "overwrite" , & tmpzval ) == SUCCESS ) {
319
+ allow_overwrite = Z_LVAL_PP (tmpzval );
320
+ }
316
321
if (result <= 299 && result >= 200 ) {
317
- errno = EEXIST ;
318
- goto errexit ;
322
+ if (allow_overwrite ) {
323
+ /* Context permits overwritting file,
324
+ so we just delete whatever's there in preparation */
325
+ php_stream_write_string (stream , "DELE " );
326
+ php_stream_write_string (stream , resource -> path );
327
+ php_stream_write_string (stream , "\r\n" );
328
+ result = GET_FTP_RESULT (stream );
329
+ if (result >= 300 || result <= 199 ) {
330
+ goto errexit ;
331
+ }
332
+ } else {
333
+ errno = EEXIST ;
334
+ goto errexit ;
335
+ }
319
336
}
320
337
}
321
338
0 commit comments