ID: 14206
Updated by: zak
Reported By: [EMAIL PROTECTED]
Old Status: Open
Status: Bogus
Bug Type: Filesystem function related
Operating System: Linux 2.2.19
PHP Version: 4.0.6
New Comment:

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.



Previous Comments:
------------------------------------------------------------------------

[2001-11-24 08:18:35] [EMAIL PROTECTED]

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:08:15] [EMAIL PROTECTED]

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 07:10:59] [EMAIL PROTECTED]

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 06:24:38] [EMAIL PROTECTED]

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) ...

------------------------------------------------------------------------



Edit this bug report at http://bugs.php.net/?id=14206&edit=1


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to