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

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系