Skip to content

Commit 5c6428b

Browse files
committed
fixed file_put_contents()
1 parent ea56c2d commit 5c6428b

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

ext/standard/file.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ PHP_FUNCTION(file_put_contents)
576576
php_stream_context *context = NULL;
577577
php_stream *srcstream = NULL;
578578
char mode[3] = "wb";
579+
char ret_ok = 1;
579580

580581
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Pz/|lr!", &filename, &filename_len, &data, &flags, &zcontext) == FAILURE) {
581582
return;
@@ -616,11 +617,13 @@ PHP_FUNCTION(file_put_contents)
616617
php_stream_truncate_set_size(stream, 0);
617618
}
618619

620+
/* In case of php_stream_write() it returns size_t, so we only can check if the full data was written.
621+
XXX maybe the write should be retried once more */
619622
switch (Z_TYPE_P(data)) {
620623
case IS_RESOURCE: {
621624
zend_str_size len;
622625
if (php_stream_copy_to_stream_ex(srcstream, stream, PHP_STREAM_COPY_ALL, &len) != SUCCESS) {
623-
numbytes = -1;
626+
ret_ok = 0;
624627
} else {
625628
numbytes = len;
626629
}
@@ -638,14 +641,13 @@ PHP_FUNCTION(file_put_contents)
638641
numbytes = php_stream_write(stream, Z_STRVAL_P(data), Z_STRSIZE_P(data));
639642
if (numbytes != Z_STRSIZE_P(data)) {
640643
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", numbytes, Z_STRSIZE_P(data));
641-
numbytes = -1;
644+
ret_ok = 0;
642645
}
643646
}
644647
break;
645648

646649
case IS_ARRAY:
647650
if (zend_hash_num_elements(Z_ARRVAL_P(data))) {
648-
zend_str_size bytes_written;
649651
zval **tmp;
650652
HashPosition pos;
651653

@@ -656,15 +658,13 @@ PHP_FUNCTION(file_put_contents)
656658
convert_to_string(*tmp);
657659
}
658660
if (Z_STRSIZE_PP(tmp)) {
661+
zend_str_size bytes_written;
662+
659663
numbytes += Z_STRSIZE_PP(tmp);
660664
bytes_written = php_stream_write(stream, Z_STRVAL_PP(tmp), Z_STRSIZE_PP(tmp));
661-
if (bytes_written < 0 || bytes_written != Z_STRSIZE_PP(tmp)) {
662-
if (bytes_written < 0) {
663-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write %d bytes to %s", Z_STRSIZE_PP(tmp), filename);
664-
} else {
665-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", bytes_written, Z_STRSIZE_PP(tmp));
666-
}
667-
numbytes = -1;
665+
if (bytes_written != Z_STRSIZE_PP(tmp)) {
666+
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to write %d bytes to %s", Z_STRSIZE_PP(tmp), filename);
667+
ret_ok = 0;
668668
break;
669669
}
670670
}
@@ -681,19 +681,19 @@ PHP_FUNCTION(file_put_contents)
681681
numbytes = php_stream_write(stream, Z_STRVAL(out), Z_STRSIZE(out));
682682
if (numbytes != Z_STRSIZE(out)) {
683683
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", numbytes, Z_STRSIZE(out));
684-
numbytes = -1;
684+
ret_ok = 0;
685685
}
686686
zval_dtor(&out);
687687
break;
688688
}
689689
}
690690
default:
691-
numbytes = -1;
691+
ret_ok = 0;
692692
break;
693693
}
694694
php_stream_close(stream);
695695

696-
if (numbytes < 0) {
696+
if (!ret_ok) {
697697
RETURN_FALSE;
698698
}
699699

0 commit comments

Comments
 (0)