|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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) ...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 22 20:00:01 2025 UTC |
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.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.