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

发布于 2024-10-05 09:22:32 字数 3565 浏览 5 评论 0原文

我最近编写了一个 php 程序,它将 CSV 编译为子 CSV 和孙 CSV 以供以后使用。但是,在某些文件中,我不断在同一位置获取 NULL 字节。当将相同的字符串写入不同的文件时,问题就消失了。我已经尝试了所有我能想到的方法,但找不到这个错误的根源。以前有人遇到过这个错误吗?

这是代码:

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

:提前

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌梦深 2024-10-12 09:22:32

没关系,我(还有其他人)已经解决了它。这是因为 Windows 文件不区分大小写...我有处理冲突的 ford-capri-colour-Black.csv 和 ford-capri-colour-black.csv 。 Windows 8-)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文