简体   繁体   中英

Force download a .csv using php

Im trying to get my webpage to take a .csv file that is stored on the server and force download it. I have tried a few different ways to accomplish this and am running into the issue that when it goes to save in chrome it downloads the .csv without issue but in safari it opens the file rather then saving it.

I tried doing this based on previous answers i saw

header('Content-Type: application/csv');
header("Content-disposition: attachment; filename='".$username.'-LMSearch'.'.csv'."'");        

readfile($filename);

But I'm not getting anything happening, so far i have just been redirecting the page to the file location. Any suggestions on where I'm going wrong or why the download isn't starting?

EDIT:

So i tried the solution that was posted and the questions that were linked to it but the problem remains...

Maybe i can explain in better detail with more code...

So A save data button is pushed,then data is being pulled from a MySQL Database and using PHPExcel it is being made into a .csv in a folder. I know that is working properly because the file exists.

    $objPHPExcel = new PHPExcel();

    $objPHPExcel->setActiveSheetIndex(0);

    // Add some data
    for ($i = 0;$i < count($dataArray);$i++){
        for ($j = 0;$j < count($dataArray[$i]);$j++){
            $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, $i+1)->getNumberFormat()->setFormatCode('0.0000');
            $objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($j, $i+1, $dataArray[$i][$j]);
            $objPHPExcel->getActiveSheet()->getColumnDimension(chr($j+65))->setAutoSize(true);
        }
    }

    $filePath = '../downloadFiles/'.$username;

    if (!file_exists($filePath)){mkdir($filePath,0700);}
    $filename = $filePath.'/'.$username.'-LMSearch-'.time().'.csv';

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV')->setDelimiter(',')
                                                                      ->setEnclosure('"')
                                                                      ->setLineEnding("\r\n")
                                                                      ->setSheetIndex(0)
                                                                      ->save($filename);
    $callEndTime = microtime(true);
    $callTime = $callEndTime - $callStartTime;


    header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename="'.$username.'-LMSearch-'.time().'.csv'.'"');
    header('Content-Type: application/octet-stream');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . filesize($filename));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Expires: 0');

    readfile($filename);
}

So at the end of this once the file is there i just want it to download directly but nothing is actually happening.

Is there a reason for this.

I do not know exactly what's going on, try my code below for example:

<?php
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$name.'"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');

readfile($file);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM