csv - PHP file writing - 0x00 appearing in files -
i have written php program compiles csv child , grandchild csvs later use. however, in files keep getting null bytes in same place. when writing same string different file problem disappears. i've tried can think of , cannot find source of bug. has come bug before?
here code:
error_reporting(e_all); $handles=array(); function get_handle($file) { global $handles; $file=unsafe($file); foreach($handles $key=>$value) { if($key==$file) return $value; } $handle=fopen($file, "w+b"); $handles[$file]=$handle; return $handle; } function safe($str) { return str_replace(array(',', "\r", "\n", '#', '-'), array('%2c', '%0d', '%0a', '%23', '%2d'), trim(trim($str), "\0")); } function additional_format($str) { return $str; } function unsafe($str) { return str_replace(array('%2c', '%0d', '%0a', '%23', '%2d'), array(',', "\r", "\n", '#', '-'), $str); } $titles=array('server', 'condition', 'vehicle', 'make', 'model'); $fhandle=fopen("input.csv", "r"); while($frow=safe(fgets($fhandle))) { if(substr($frow, 0, 7)!='http://') continue; $url=array_map('safe', explode('/', substr($frow, 7))); $additional=array_map('safe', array_slice($url, 5)); $additional_string=implode(" ", $additional); $url=array_combine($titles, array_slice($url, 0, 5)); $make=get_handle($url['make'].".csv"); fwrite($make, ucwords($url['model'])."\r\n"); fflush($make); $variation_types=array(); for($addi=0;isset($additional[$addi])&&!empty($additional[$addi]);$addi+=2) { if(!isset($variation_types[$additional[$addi]])) { $variation_types[$additional[$addi]]=array(); } if(!in_array($additional[$addi+1], $variation_types[$additional[$addi]])) { array_push($variation_types[$additional[$addi]], $additional[$addi+1]); } $variation_file=get_handle($url['make'].'-'.$url['model'].'-'.$additional[$addi].'-'.$additional[$addi+1].".csv"); fwrite( $variation_file, trim($frow, ',').",".strtolower($url['model']." ".$additional_string."\r\n") ); fflush($variation_file); if(1) fwrite(get_handle("test.csv"), trim($frow, ',').",".$url['model']." ".$additional_string."\r\n"); } $model=get_handle($url['make'].'-'.$url['model'].".csv"); foreach($variation_types $type=>$variations) { fwrite($model, $type.','.implode(',', $variations)."\r\n"); fflush($model); } }
a sample input csv (the real files larger):
http://server/used/cars/ford/capri/trim/cc-2 http://server/used/cars/ford/capri/engine/2.0 http://server/used/cars/ford/capri/fuel/petrol http://server/used/cars/ford/capri/transmission/manual http://server/used/cars/ford/capri/colour/black http://server/used/cars/ford/capri/colour/blue http://server/used/cars/ford/capri/colour/gold http://server/used/cars/ford/capri/colour/purple http://server/used/cars/ford/capri/colour/red http://server/used/cars/ford/capri/colour/silver
and input in format (-, #, comma, \r , \n urlencoded) end like:
http:/server/used/cars/ford/capri/colour/black,capri colour black ##0x00 times 2549##http://server/used/cars/ford/capri/trim/cc%2d2/colour/black/door/2,capri trim cc%2d2 colour black door 2 http://server/used/cars/ford/capri/bodystyle/convertible/colour/black/door/2,capri bodystyle convertible colour black door 2 http://server/used/cars/ford/capri/engine/2.0/colour/black/door/2,capri engine 2.0 colour black door 2 http://server/used/cars/ford/capri/fuel/petrol/colour/black/door/2,capri fuel petrol colour black door 2 http://server/used/cars/ford/capri/transmission/manual/colour/black/door/2,capri transmission manual colour black door 2
thanks in advance
[edit] record, removing trim($str, "\0") makes no difference , php version 5.3.1 (cli)
never mind, ive (well else) has solved it. because of windows file case-insensitivity... had handles ford-capri-colour-black.csv , ford-capri-colour-black.csv colliding. windows 8-)
Comments
Post a Comment