php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78976 SplFileObject::fputcsv returns -1 on failure
Submitted: 2019-12-17 07:39 UTC Modified: 2019-12-17 16:42 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: nyamsprod at gmail dot com Assigned: cmb (profile)
Status: Closed Package: SPL related
PHP Version: 7.4.0 OS:
Private report: No CVE-ID: None
 [2019-12-17 07:39 UTC] nyamsprod at gmail dot com
Description:
------------
The behavior of fwrite/fputcsv and SplFileObject::fwrite and SplFileObject::fputcsv have changed in PHP7.4 when used with a non writable stream. 

see https://3v4l.org/ifpaa

Reading the changelog I fail to see where this is mention so is it the now expected behaviour from PHP7.4+ or is it a bug ?

Test script:
---------------
<?php

$spl = new SplFileObject('php://temp', 'r');
$fh = fopen('php://temp', 'r');

var_export([
    'SplFileObject::fputcsv result on failure' => $spl->fputcsv(['foo', 'bar']),
    'SplFileObject::fwrite result on failure' => $spl->fwrite('foo'),
    'fputcsv result on failure' => fputcsv($fh, ['foo', 'bar']),
    'fwrite result on failure' => fwrite($fh, 'foo'),
]);

Expected result:
----------------
array (
  'SplFileObject::fputcsv result on failure' => 0,
  'SplFileObject::fwrite result on failure' => 0,
  'fputcsv result on failure' => 0,
  'fwrite result on failure' => 0,
)

Actual result:
--------------
array (
  'SplFileObject::fputcsv result on failure' => -1,
  'SplFileObject::fwrite result on failure' => false,
  'fputcsv result on failure' => false,
  'fwrite result on failure' => false,
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-17 12:36 UTC] nyamsprod at gmail dot com
Seems this BC is documented but the fact that I get -1 instead of false when using SplFileObject::fputcsv is the real bug.
 [2019-12-17 16:41 UTC] cmb@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2019-12-17 16:41 UTC] cmb@php.net
> Seems this BC is documented […]

Indeed[1].

> but the fact that I get -1 instead of false when using
> SplFileObject::fputcsv is the real bug.

Yes.  Thanks for reporting!

[1] <https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.core.fread-fwrite>
 [2019-12-17 16:42 UTC] cmb@php.net
-Summary: fputcsv and fwrite behavior changed from previous version on failure +Summary: SplFileObject::fputcsv returns -1 on failure
 [2019-12-17 16:54 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4cd6d384bde0f0641e88ae5431d5b6bdfff4ac83
Log: Fix #78976: SplFileObject::fputcsv returns -1 on failure
 [2019-12-17 16:54 UTC] cmb@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 09:01:32 2024 UTC