开发者

PHP file writing - 0x00 appearing in files

I have recently written a php program that compiles a CSV into child and grandchild CSVs for later use. However, in certain files I keep getting NULL bytes in the same place. When writing the same string to a different file the problem disappears. I've tried everything I can think of and cannot find the source of this bug. Has anyone come into this bug before?

here is the code:

error_reporting(E_ALL);
$handles=array();

function get_handle($file) {
 global $handles;
 $file=unsafe($file);

 foreach($handles as $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 as $type=>$variations) {
  fwrite($model, $type.','.implode(',', $variations)."\r\n");
  fflush($model);
 }

}

a sample input CSV would be (the real files are much 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 from input in this format (-, #, comma, \r and \n get urlencoded) i end up with something 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] for the record, removing the trim($str, "\0") makes no difference and my php version is 5.3.1 (cli)


never mind, ive (well someone else) has solved it. It was because of windows file case-insensitivity... I had handles to ford-capri-colour-Black.csv and ford-capri-colour-black.csv which were colliding. Windows 8-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜