Skip to content

Commit cb89565

Browse files
committed
Feature Request #7121.
Allow overwritting of files via ftp:// wrapper. Requires context option: $context['ftp']['overwrite'] != 0
1 parent c523fc4 commit cb89565

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

ext/standard/ftp_fopen_wrapper.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, char *path, ch
146146
php_stream *reuseid=NULL;
147147
char *tpath, *ttpath, *hoststart=NULL;
148148
size_t file_size = 0;
149+
zval **tmpzval;
150+
int allow_overwrite = 0;
149151

150152
tmp_line[0] = '\0';
151153

@@ -312,10 +314,25 @@ php_stream * php_stream_url_wrap_ftp(php_stream_wrapper *wrapper, char *path, ch
312314
php_stream_notify_file_size(context, file_size, tmp_line, result);
313315
}
314316
} 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+
}
316321
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+
}
319336
}
320337
}
321338

0 commit comments

Comments
 (0)