dongwen3093 2018-05-02 05:36
浏览 299

如何进行文件下载而不是保存在服务器上?

  $filename = "PM.xls";
  $exists = file_exists('PM.xls');
  if($exists)
  {
           unlink($filename);
  }
           $filename = "PM.xls";
           $fp = fopen($filename, "wb");
           $sql = "SELECT DATE_FORMAT(jobcard.Open_date_time,' %d-%b-%y') AS datee,vehicles_data.Frame_no, jobcard.Jobc_id,jobcard.serv_nature,jobcard.Customer_name,jobc_invoice.Lnet, jobc_invoice.Pnet, jobc_invoice.Snet, jobcard.Mileage,customer_data.cust_type,vehicles_data.model_year,jobcard.Veh_reg_no,jobcard.comp_appointed, customer_data.mobile,IF(variant_codes.Make IS NULL,'Others',variant_codes.Make) as make FROM `jobcard` LEFT OUTER JOIN jobc_invoice ON jobcard.Jobc_id=jobc_invoice.Jobc_id LEFT OUTER JOIN vehicles_data ON jobcard.Vehicle_id=vehicles_data.Vehicle_id LEFT OUTER JOIN variant_codes ON vehicles_data.Model=variant_codes.Model LEFT OUTER JOIN customer_data ON jobcard.Customer_id=customer_data.Customer_id ORDER BY `make` ASC";

           $result=mysqli_query($conn,$sql) or die(mysqli_error($sql));
           $schema_insert = "";
           $schema_insert_rows = "";

           while($row = mysqli_fetch_row($result))
           {
            $insert = $row[0]. "\t" .$row[1]. "\t".$row[2]. "\t".$row[3]. "\t".$row[4]. "\t".$row[5]. "\t".$row[6]. "\t".$row[7]. "\t".$row[8]. "\t".$row[9]. "\t".$row[10]. "\t".$row[11]. "\t".$row[12]. "\t".$row[13]. "\t".$row[14]. "\t".$row[15];
           $insert .= "
";               //       serialize($assoc)
           fwrite($fp, $insert);
           }

Above code successfully creates the file on server but can't replace it, Instead of creating the file on server i want it download the file on user end. Should I use any excel library as I have to name excel sheet as well.

  • 写回答

1条回答 默认 最新

  • drfif48428 2018-05-03 07:55
    关注

    First, you creating TSV file with XLS extension. If you need to create XLS document, you should use PHPExcel or so.

    If CSV is enough for you, there are fputcsv available since 5.1 and you can stream file data directly to the user agent.

    $filename = "PM.csv";
    
    header('Content-Description: File Transfer');
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="'.addcslashes($filename, '\\"').'"');
    $fp = fopen('php://output', 'wb');
    
    // ...
    
    while($row = mysqli_fetch_row($result))
    {
        fputcsv($fp, $row, ',', '"');
    }
    fclose($fp);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊