使用Dos\Windows对文本文件进行编码,并从PHP以UTF-8编码

使用Dos\Windows对文本文件进行编码,并从PHP以UTF-8编码,php,file-io,utf-8,Php,File Io,Utf 8,我在linux上创建一个文本文件时遇到问题,该文件的格式为Dos\Windows,编码为UTF-8 我所做的是: while($row = mysql_fetch_array($result)) { $product = $row['products_id'] . ' ' . $row['products_name'] . ' ' . $row['manufacturers_name'] . ' ' . $row['products_model'] //. ' ' . $

我在linux上创建一个文本文件时遇到问题,该文件的格式为Dos\Windows,编码为UTF-8

我所做的是:

while($row = mysql_fetch_array($result))
 {
  $product =  $row['products_id']
  . ' ' . $row['products_name']
  . ' ' . $row['manufacturers_name']
  . ' ' . $row['products_model']
  //. ' ' . $row['products_sku']
  . ' ' . zen_href_link('product_info', 'cPath=' . $cPath .  '&products_id=' . $row['products_id']) 
  . ' ' . number_format($row['products_price'],2)
  . ' ' . strip_tags(zen_get_products_description($row['products_id']))
  . ' ' . HTTP_SERVER . '/' .DIR_WS_IMAGES . $row['products_image']
  . ' ' . 'electronics'
  . "\r\n";
 $product = utf8_encode($product);
 fwrite($fh, gzencode($product,9)); 

我有这样的印象\r\n编码完成后就可以了。我在这里遗漏了什么吗?

我怀疑你是否想像gz那样一个记录一个记录地编码它

如果你需要对它进行格式化,考虑整理它,然后在结尾写一次,或者格子化文件后创建。< /P>

$productList = '';

while($row = mysql_fetch_array($result)) {
  $product =  $row['products_id']
  . ' ' . $row['products_name']
  . ' ' . $row['manufacturers_name']
  . ' ' . $row['products_model']
  //. ' ' . $row['products_sku']
  . ' ' . zen_href_link('product_info', 'cPath=' . $cPath .  '&products_id=' . $row['products_id']) 
  . ' ' . number_format($row['products_price'],2)
  . ' ' . strip_tags(zen_get_products_description($row['products_id']))
  . ' ' . HTTP_SERVER . '/' .DIR_WS_IMAGES . $row['products_image']
  . ' ' . 'electronics'
  . "\r\n";

  $productList .= $product;
}

fwrite($fh, gzencode(utf8_encode($product),9));
fclose($fh);
错误信息\r\n是正确的。如果数据库表已经包含UTF-8,则utf8_编码是冗余的。但我看到的真正问题是,您应该只在while{…}循环中包装$product变量的串联。不能为每一行重新启动gzencode并将块写入文件。当您试图打开它时,这将导致错误。不要多次调用gzencode,只需使用gzopen而不是fopen打开输出文件即可