我使用以下脚本通过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值为“/home/demo/public_html/testwordpress/csv/sale.csv” 还尝试了标题(“Content-type:text / csv”);
不知何故,它没有下载路径上给出的csv文件。它是在csv中下载页面的源代码。 这是我得到的代码:
<!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" />
请告知
答案 0 :(得分: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);
答案 1 :(得分: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";
?>