从PHP下载Excel文件

时间:2019-06-11 22:10:35

标签: php mysql header phpexcel

我在一个将文件存储在Mysql数据库中的项目中工作。

我将文件存储为长blob,并将其存储在数据库中,并且希望使它们在基于PHP的站点网络中可读。我试图查看PDF文件,但是它可以工作,但是如果我尝试下载Excel File,它会给我file.php和一些数字。

//PHP

$dbh= new PDO("mysql:host=localhost;dbname=smi","root","");
$id=isset($_GET['id_fichier'])? $_GET['id_fichier']:"";
$stat=$dbh->prepare("select * from fichier where id_fichier=?");
$stat->bindparam(1,$id);
$stat->execute();
$row=$stat->fetch();
$file = $row['fichier'];
$type=$row['type'];
header('Content-Type:'.$type);
echo($file);

我希望使用excel文件,但会得到类似的内容。

3c21 444f 4354 5950 4520 6874 6d6c 3e0d
0a3c 6874 6d6c 2064 6972 3d22 6c74 7222
206c 616e 673d 2265 6e22 3e0d 0a3c 6865
6164 3e0d 0a3c 7469 746c 653e 534d 4920
4165 726f 706f 7274 2046 6573 2053 6169
7373 3c2f 7469 746c 653e 0d0a 0d0a 3c2f

1 个答案:

答案 0 :(得分:-1)

确保已将其正确添加到数据库中,并且列ThisWorkbook.Worksheets("Data").Range("A1:A7").Formula = "=average(" & str2 & ")" 的类型设置为fichier,并且没有其他设置。

以下是干净的代码,可将其插入数据库:

BLOB

然后再选择一次:

$dbh = new PDO('mysql:host=localhost;dbname=smi', 'root', '');

$file = 'XYZ.xls';
$blob = fopen($file, 'rb');
$mime = 'application/vnd.ms-excel';

$sql  = "INSERT INTO `fichier` (fichier, type) VALUES (:fichier, :type)";
$stmt = $dbh->prepare($sql);
$stmt->bindParam(':fichier', $blob, PDO::PARAM_LOB);
$stmt->bindParam(':type', $mime);
$stmt->execute();