在使用fwrite写入文件时,输出文件在文件的最后一行附加文件大小

时间:2012-04-19 05:02:54

标签: php csv fopen fwrite

我正在尝试使用php编写和下载CSV文件。因此,每次创建文件时,都会在最后一行附加文件大小。比如,如果文件大小是10kb,那么文件结果是

    Viwed Date  Product Name    Viwed Item  Username    User Email
    16-04-2012  i3 Primary  Mathematics neha_new    neha_new@mailinator.com
    16-04-2012  i3 Primary  III         neha_new    neha_new@mailinator.com
    16-04-2012  i3 Primary  i3 Primary  neha_new    neha_new@mailinator.com

这是代码

if($_POST['report_format']=='csv') {
  $csv_rows[] = array('Viewed Date','Product Name','Viewed Item','Username','User Email');
  $da = db_query($sql);
  while($data = db_fetch_object($da)) {
    $partid = db_query("select name from term_data where tid=".$data->ptid);
    $product_name = db_result($partid);

if($data->type=='term') {
  $catid = db_query("select name from term_data where tid=".$data->tid);
  $cat_name = db_result($catid);
  }  elseif($data->type=='node') {
    $term_node = node_load($data->tid);
$cat_name  = $term_node->title;
  }
  else {
$term_node = node_load($data->tid);
$cat_name  = $term_node->field_read_image_field[0]['filename'];
  }
  $u = user_load($data->uid);
  csv_rows[] = array(date('d-m-Y', $data->date), $product_name, $cat_name, $u->name, $u->mail);
}
foreach($csv_rows as $csvrow) {
  $csvdata .= implode(',',$csvrow)."\n";
}
$file_path = $base_url."/sites/default/files/";
$fp = fopen("sites/default/files/user_csv/account_report.csv", "w") or exit("Unable to open file!");
fwrite($fp, $csvdata,strlen($csvdata)-1);
fclose($fp);

$url = $base_url.'/sites/default/files/user_csv/report_csv.php';
header("Location: ".$url);

    }

0 个答案:

没有答案