0

I am using following script to download a file via PHP:

header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=sale.csv');
    header("Content-Transfer-Encoding: binary");
    header('Expires: 0');
    header('Pragma: no-cache');
    header("Content-Length: ".filesize($CSVFileName));
        readfile($CSVFileName);

$CSVFilename value is "/home/demo/public_html/testwordpress/csv/sale.csv" Also tried header("Content-type: text/csv");

Somehow it is not downloading the csv file given on the path. It is downloading source code of the page in csv. Here is the code i am getting:

<!DOCTYPE html>
<!--[if IE 6]>
<html id="ie6" lang="en-US">
<![endif]-->
<!--[if IE 7]>
<html id="ie7" lang="en-US">
<![endif]-->
<!--[if IE 8]>
<html id="ie8" lang="en-US">
<![endif]-->
<!--[if !(IE 6) | !(IE 7) | !(IE 8)  ]><!-->
<html lang="en-US">
<!--<![endif]-->
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>Call Details | Test</title>
<link rel="profile" href="http://gmpg.org/xfn/11" />

Please advise

4

2 回答 2

1

我在这里可能有点偏离我的想法,但有一种更简单的方法(我认为!)来简单地下载文件。

$file = "/home/demo/public_html/testwordpress/csv/sale.csv";
$contents = file_get_contents($file);

// Manipulate the contents however you wish.
$newFilePath = "somewhere/over/the/rainbow/sale.csv";
file_put_contents($newFilePath, $contents);
  • file_get_contents 也适用于 http 流。
于 2013-09-06T16:44:14.770 回答
0

我是这样做的:

<?PHP
    $filename= "yourfilepath.csv";

    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: application/vnd.ms-excel");

    echo "your content to the .csv file";
?>
于 2013-09-06T15:48:02.000 回答