doutan1671 2015-11-02 12:41
浏览 51
已采纳

PHP:在流标题处于函数中时写入CSV文件

I'm attempting to write an SQL query to a CSV file in PHP. I have had this this working, thanks to some advice [in a previous question][1]. However I'm attempting to reduce repetition in my code were possible from the previous example.

I'm creating headers/stream wrapper in a function here:

function csvCreate($filename){
    header("Cache=Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-Description: File Transfer');
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename={$filename}");
    header("Expires: 0");
    header("Pragma: public");
    $fh = @fopen( 'php://output', 'w' );
}

I'm then calling the function in a conditional statement and passing $filename as a parameter here:

if(isset($_POST["Submit"]) && ($_POST['Weight'] == 'Weight'))
{
    //csvCreate->$fileName .= 'OutputWeightNull.csv';
    csvCreate('OutputWeightNull.csv');
    $query = mysqli_query($conn, 'SELECT * FROM  `bfi_product_volumne_data` WHERE weight = 0 OR weight IS NULL') or die(mysqli_error($conn));
    if ($result = mysqli_fetch_array($query,MYSQLI_ASSOC))
    {
        fputcsv($fh, array_keys($result));
        fputcsv($fh, $result);
        while ($result = mysqli_fetch_array($query,MYSQLI_ASSOC))
        {
            fputcsv($fh, $result);
        }
    }
}

The problem is data is not being written to the CSV. My feeling is I need to point the 'fputcsv' call to the function. I've attempted this using 'csvCreate->fputcsv' which doesn't appear to work. Could anyone suggest what the issue could be here. Thanks

  [1]: http://stackoverflow.com/questions/33433737/php-writing-a-mysql-query-to-cs
  • 写回答

1条回答 默认 最新

  • drkbpwk609294 2015-11-02 12:45
    关注

    The problem is that you create file pointer $fh inside the function. So it is only visible inside the function and not outside of the function (as it probably was before you put the code to the function). You need to pass that variable from the function to the outer world. It can be done with return. Then in the 'outer' world you need to assign that returned value to the variable in the 'outer' scope and use it.

    You need to pass $fh outside of the function. I.e.

    function csvCreate($filename){
        // .. rest of the code
        $fh = @fopen( 'php://output', 'w' );
        return $fh;
    }
    

    then in the code where you write to output do the following making $fh visible in that scope.

    // .. rest of the code
    $fh = csvCreate('OutputWeightNull.csv');
    // .. rest of the code
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)