PHP文件写入 - 0x00出现在文件中

时间:2010-12-02 10:07:24

标签: php csv null fwrite

我最近写了一个php程序,它将CSV编译成子孙CSV,供以后使用。但是,在某些文件中,我在同一个地方继续获得NULL字节。将相同的字符串写入不同的文件时,问题就会消失。我已经尝试了所有我能想到的东西,却无法找到这个bug的来源。有人之前有过这个错误吗?

这是代码:

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);
 }

}

示例输入CSV将是(真实文件更大):

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

从这种格式的输入( - ,#,逗号,\ r和\ n获取urlencoded)我最终得到的结果如下:

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

提前致谢

[edit]记录,删除修剪($ str,“\ 0”)没有区别,我的php版本是5.3.1(cli)

1 个答案:

答案 0 :(得分:0)

没关系,我(好吧别人)解决了它。这是因为Windows文件不区分大小写...我有处理到ford-capri-color-Black.csv和ford-capri-color-black.csv的碰撞。 Windows 8 - )