doucuodan0897 2016-03-23 16:25
浏览 99
已采纳

fputcsv创建文件但将其下载为空

I have this piece of PHP code that's intended to retrieve data from a mySQL database, and export it to a CSV file that has to be automatically downloaded after it was created.

$connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));

// fetch mysql table rows
$sql = "select * from users";
$result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));

$fp = fopen('users.csv', 'w');

while($row = mysqli_fetch_assoc($result)) {
    fputcsv($fp, $row);
}

fclose($fp);

header('Content-Type: text/csv');

header('Content-Disposition: attachment; filename="users.csv"');

mysqli_close($connection);

The problem here is that it:

  • Retrieves the data.
  • Retrieves the CSV file on the server in the same directory of the export.php file with the data on it.
  • Downloads the file with the same name BUT it's EMPTY

Thanks.

  • 写回答

1条回答 默认 最新

  • dongqin8652 2016-03-23 16:32
    关注

    You're writing it to a file called users.csv, but the file you are forcing the user to download is the output of the page.

    As long as your query is correct, once the PHP script has run, there should be a file called users.csv in the same directory as the PHP file that contains the correct data.

    You need to output the data to the browser for it to be attributed to the file you're downloading.

    Try this:

    //Connect to database
    $connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
    
    //Fetch mysql table rows
    $sql = "select * from users";
    $result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));
    
    //Close connection
    mysqli_close($connection);
    
    //Set $output
    $output = "";
    
    //Set header values
    $headers = array("Header 1", "Header 2", "Header 3");
    
    //Insert header values to $output
    foreach($headers as $h){
        $output .= fieldCheck($h) . ",";
    }
    $output = rtrim($output, ","). "
    ";
    
    //Iterate through results
    while($row = mysqli_fetch_assoc($result)) {
        foreach($row as $cell){
            //Comma-separate each value
            $output .= fieldCheck($cell).",";
        }
        //Remove last comma of each line and add newline
        $output = rtrim($output, ",") . "
    ";
    }
    
    //Set headers
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="users.csv"');
    
    //Output
    echo $output;
    
    exit;
    
    //Function in case of comma in field
    function fieldCheck($string){
        if(strpos($string, ",") !== false){
            $string = '"'.$string.'"';
        }
        return $string;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口