@@ -576,6 +576,7 @@ PHP_FUNCTION(file_put_contents)
576
576
php_stream_context * context = NULL ;
577
577
php_stream * srcstream = NULL ;
578
578
char mode [3 ] = "wb" ;
579
+ char ret_ok = 1 ;
579
580
580
581
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "Pz/|lr!" , & filename , & filename_len , & data , & flags , & zcontext ) == FAILURE ) {
581
582
return ;
@@ -616,11 +617,13 @@ PHP_FUNCTION(file_put_contents)
616
617
php_stream_truncate_set_size (stream , 0 );
617
618
}
618
619
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 */
619
622
switch (Z_TYPE_P (data )) {
620
623
case IS_RESOURCE : {
621
624
zend_str_size len ;
622
625
if (php_stream_copy_to_stream_ex (srcstream , stream , PHP_STREAM_COPY_ALL , & len ) != SUCCESS ) {
623
- numbytes = -1 ;
626
+ ret_ok = 0 ;
624
627
} else {
625
628
numbytes = len ;
626
629
}
@@ -638,14 +641,13 @@ PHP_FUNCTION(file_put_contents)
638
641
numbytes = php_stream_write (stream , Z_STRVAL_P (data ), Z_STRSIZE_P (data ));
639
642
if (numbytes != Z_STRSIZE_P (data )) {
640
643
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 ;
642
645
}
643
646
}
644
647
break ;
645
648
646
649
case IS_ARRAY :
647
650
if (zend_hash_num_elements (Z_ARRVAL_P (data ))) {
648
- zend_str_size bytes_written ;
649
651
zval * * tmp ;
650
652
HashPosition pos ;
651
653
@@ -656,15 +658,13 @@ PHP_FUNCTION(file_put_contents)
656
658
convert_to_string (* tmp );
657
659
}
658
660
if (Z_STRSIZE_PP (tmp )) {
661
+ zend_str_size bytes_written ;
662
+
659
663
numbytes += Z_STRSIZE_PP (tmp );
660
664
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 ;
668
668
break ;
669
669
}
670
670
}
@@ -681,19 +681,19 @@ PHP_FUNCTION(file_put_contents)
681
681
numbytes = php_stream_write (stream , Z_STRVAL (out ), Z_STRSIZE (out ));
682
682
if (numbytes != Z_STRSIZE (out )) {
683
683
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 ;
685
685
}
686
686
zval_dtor (& out );
687
687
break ;
688
688
}
689
689
}
690
690
default :
691
- numbytes = -1 ;
691
+ ret_ok = 0 ;
692
692
break ;
693
693
}
694
694
php_stream_close (stream );
695
695
696
- if (numbytes < 0 ) {
696
+ if (! ret_ok ) {
697
697
RETURN_FALSE ;
698
698
}
699
699
0 commit comments