php - fwrite adds an empty line at textfile -
i have problem fwrite
. do. data textfile print table. after print values user should able select data should deleted (i key). when he/she clicks on delete file post key has deleted. of works. update array deleting key , works. data has rewritten textfile file adds space causes array have "undefined offset" error. have rid of blank line. code :
<?php // opening required file include("deletefunc.php"); $obj = new deletefunc(); $result = $obj->getmedata(); // posted value (the key has deleted array) $deletekey = $_post['rownumber']; $updatearray = array(); for($i=0; $i<count($result); $i++){ if($i==$deletekey){ } else{ $updatearray[$i] = $result[$i]; } } var_dump($updatearray); // deleting data form textfile (replacing) $filename="phones.txt"; $file = fopen($filename,"w"); foreach($updatearray $row){ fwrite($file, $row); } ?>
this guess :
your problem comes fact element (except last one) ending endofline character... when remove element in middle, no problem, if remove last one, 1 before has endofline character, , therefore have empty line in file.
so should add endofline character when writing file, , not keeping them in elements.
to so, should use php_eol
predefined constant end of line character adapted platform.
and if wondering looking @ question, fwrite()
php function not add eol characters.
Comments
Post a Comment