doulin2555 2015-08-31 13:32
浏览 73
已采纳

PHP下载一个csv文件然后创建2个文件

So, whenever the user downloads a .csv file from the site, another .csv file gets created to my ../includes folder so it means there is a .csv file download located inside the downloads folder and another .csv file created where my .php files are located. Is there any way to prevent the file inside my .php files to be created and instead only the download? thanks! here's the code.

<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "envoy");
function db_connect() {

  $db = mysqli_connect(DB_HOST, DB_USER, DB_PASS) or die("ERROR!");
  mysqli_select_db($db, DB_NAME);

  return $db;
}

$db = db_connect();
date_default_timezone_set("EST5EDT");
$date = date('m-d-Y,h.i.sa');
$date2 = date('m/d/Y');
$filename = $date.'.csv';
$fp = fopen($filename,"w");

$sql2 = "SELECT sku as Sku,name as 'Brochure Name',location as Location FROM brochureinfo where modified LIKE '$date2' ORDER BY name  ";
$rs2 = mysqli_query($db, $sql2);
mysqli_close($db);

$row = mysqli_fetch_assoc($rs2);

$seperator = "";
$comma = "";

foreach ($row as $name => $value){
    $seperator.= $comma. ''.str_replace('','""',$name);
    $comma=",";

}
$seperator .= "
";

echo $seperator;




mysqli_data_seek($rs2,0);

while ($row = mysqli_fetch_assoc($rs2)){

$seperator = "";
$comma = "";

foreach ($row as $name => $value){

            $value = str_replace( array( "" , "
", "
", "
" ) ,'' , $value);
            $value = str_replace('</b><br>','',$value);
            $value = str_replace('<b>','',$value);
            $value = str_replace('<br>','',$value);
            $value = str_replace('<br />','',$value);

    $seperator.= $comma. ''.str_replace('','""',$value);
    $comma=",";

    }
$seperator .= "
";

//putting the heading into the csv file
fputs($fp,$seperator);

}

header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=$date.csv");
header('Pragma: no-cache');
readfile($filename);

fclose($fp);

if it cannot be prevented, maybe a way to change the location where it is created instead of my includes folder maybe another one? Thank you very much!

  • 写回答

2条回答 默认 最新

  • douke9379 2015-08-31 13:44
    关注

    This happens because the code saves the CSV to a local file, then reads the file and sends it to the browser. To skip the local creation, comment/remove the fopen(), fputs(), fclose() lines, comment out/remove the $seperator = ""; line and replace readfile($filename); with echo $seperator; Also, you have a echo $seperator; earlier in the code that has to be removed/commented.

    Making your code something like:

    <?php
    define("DB_HOST", "localhost");
    define("DB_USER", "root");
    define("DB_PASS", "");
    define("DB_NAME", "envoy");
    function db_connect() {
    
      $db = mysqli_connect(DB_HOST, DB_USER, DB_PASS) or die("ERROR!");
      mysqli_select_db($db, DB_NAME);
    
      return $db;
    }
    
    $db = db_connect();
    date_default_timezone_set("EST5EDT");
    $date = date('m-d-Y,h.i.sa');
    $date2 = date('m/d/Y');
    $filename = $date.'.csv';
    //$fp = fopen($filename,"w");
    
    $sql2 = "SELECT sku as Sku,name as 'Brochure Name',location as Location FROM brochureinfo where modified LIKE '$date2' ORDER BY name  ";
    $rs2 = mysqli_query($db, $sql2);
    mysqli_close($db);
    
    $row = mysqli_fetch_assoc($rs2);
    
    $seperator = "";
    $comma = "";
    
    foreach ($row as $name => $value){
        $seperator.= $comma. ''.str_replace('','""',$name);
        $comma=",";
    
    }
    $seperator .= "
    ";
    
    //echo $seperator;
    
    
    
    
    mysqli_data_seek($rs2,0);
    
    while ($row = mysqli_fetch_assoc($rs2)){
    
    //$seperator = "";
    $comma = "";
    
    foreach ($row as $name => $value){
    
                $value = str_replace( array( "" , "
    ", "
    ", "
    " ) ,'' , $value);
                $value = str_replace('</b><br>','',$value);
                $value = str_replace('<b>','',$value);
                $value = str_replace('<br>','',$value);
                $value = str_replace('<br />','',$value);
    
        $seperator.= $comma. ''.str_replace('','""',$value);
        $comma=",";
    
        }
    $seperator .= "
    ";
    
    //putting the heading into the csv file
    //fputs($fp,$seperator);
    
    }
    
    header('Content-Type: text/csv');
    header("Content-Disposition: attachment; filename=$date.csv");
    header('Pragma: no-cache');
    echo $seperator;
    
    //fclose($fp);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)