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

报告相同问题?