php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14206 fputs(); it cuts out any newline codes
Submitted: 2001-11-24 06:24 UTC Modified: 2001-11-24 08:23 UTC
From: dstankewitz at dragonsphere dot de Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 4.0.6 OS: Linux 2.2.19
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dstankewitz at dragonsphere dot de
New email:
PHP Version: OS:

 

 [2001-11-24 06:24 UTC] dstankewitz at dragonsphere dot de
Hi,

i just found a very strange bug. Im using something like this in a class:

function csv_Append ($file, $elements, $separator = ';') {

  $fp = fopen($file, 'a');
  if (!$locked = flock($fp, LOCK_EX)) return false;
  $out = implode($separator, $elements)."\n";
  fputs($fp, $out);
  flock($fp, LOCK_UN);
  fclose($fp);
  
  return true;
  
} //-- end function

When using this function fputs() cuts out all escaped codes like \n \r \t .... even if i try to write it directly (chr(13)) and i check the file with a hex editor the is no 0A  byte... but fputs reports it has written one byte (returns 1) ...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-24 07:10 UTC] mfischer@php.net
Can't reproduce this with 4.0.6 CGI und current CVS version.

Please provide a independent runnning script with its own input data (i.e. so its for the testers just a matter of copy&paste and run).

If in doubt also try latest RC from
http://www.php.net/~zeev/php-4.1.0RC3.tar.gz

Feedback.
 [2001-11-24 08:08 UTC] dstankewitz at dragonsphere dot de
Okay, first create a file called test.csv containing these lines:

value1;10;value3;thatsall
value2;10;value3;thatsall
value3;10;value3;thatsall

then c&p a file (test.php) like this:

<?
  
function csv_Update ($file, $match, $replace, $separator = ';') {

  $lines = array();
  $fp = fopen($file, 'r+');
  if (!$locked = flock($fp, LOCK_SH + LOCK_EX)) return false;
  while (!feof($fp)) {
    $buffer = trim(fgets($fp, 4096));
    if (strlen($buffer) > 1 && $buffer[0] != '#') {
      $line = explode($separator, $buffer);
      if ($line[$match[0]] == $match[1]) {
        $line[$replace[0]] = $replace[1];
        $buffer = implode($separator, $line);
      }
      $lines[] = $buffer;
    }
  }
  
  fseek($fp, 0);
  if (!ftruncate($fp, 0)) return false;
  
  foreach ($lines as $line) fputs($fp, $line)."\n";
  flock ($fp, LOCK_UN);    
  fclose ($fp);
  
  return true;

} //-- end function
  
class usr_authorize {

  function Authorize() {

    //-- Update user-file
    csv_Update('test.csv', array(0, 'value2'), array(1, date('U')));
    
    return true;

  } //-- end function

} //-- end class


$login = new usr_authorize();
$login->Authorize();

?>

the script should update the line containing "value2" as 1st csv field and replace the 2nd value with the current timestamp. so it doest, but it removes all newlines from the csv file.
 [2001-11-24 08:18 UTC] dstankewitz at dragonsphere dot de
Okay, first create a file called test.csv containing these lines:

value1;10;value3;thatsall
value2;10;value3;thatsall
value3;10;value3;thatsall

then c&p a file (test.php) like this:

<?
  
function csv_Update ($file, $match, $replace, $separator = ';') {

  $lines = array();
  $fp = fopen($file, 'r+');
  if (!$locked = flock($fp, LOCK_SH + LOCK_EX)) return false;
  while (!feof($fp)) {
    $buffer = trim(fgets($fp, 4096));
    if (strlen($buffer) > 1 && $buffer[0] != '#') {
      $line = explode($separator, $buffer);
      if ($line[$match[0]] == $match[1]) {
        $line[$replace[0]] = $replace[1];
        $buffer = implode($separator, $line);
      }
      $lines[] = $buffer;
    }
  }
  
  fseek($fp, 0);
  if (!ftruncate($fp, 0)) return false;
  
  foreach ($lines as $line) fputs($fp, $line)."\n";
  flock ($fp, LOCK_UN);    
  fclose ($fp);
  
  return true;

} //-- end function
  
class usr_authorize {

  function Authorize() {

    //-- Update user-file
    csv_Update('test.csv', array(0, 'value2'), array(1, date('U')));
    
    return true;

  } //-- end function

} //-- end class


$login = new usr_authorize();
$login->Authorize();

?>

the script should update the line containing "value2" as 1st csv field and replace the 2nd value with the current timestamp. so it doest, but it removes all newlines from the csv file.
 [2001-11-24 08:23 UTC] zak@php.net
Sorry, but this really looks like a bug in your code, not 
in PHP.

You are trimming off the newline characters in line 9 and 
then you are adding a newline to the end of a call to 
fputs on line 23 (fputs($fp, $line)."\n";)

Move the newline (fputs($fp, $line."\n");) into the call 
to fputs and your code should work as anticipated.


 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Jun 15 11:01:24 2021 UTC