有件事情我就是想不通:我已经在网页上成功地建立了上传功能,可以将文件上传到MySQL数据库。当我在服务器上通过phpMyAdmin打开它们时,它们看起来都很好...... txt、jpg、pdf等。
然而,在我写下面这个下载功能后,出现了一个奇怪的问题:所有文本文档(以及所有其他类型的文件,在我将扩展名改为'txt'后)都包含页面本身的HTML代码,后跟原始内容!
此外,在POST之后,不同的浏览器显示的方式也不同。当尝试下载txt文件时,IE会在页面的ECHO中显示正确的数据(没有下载),并在其前面显示错误消息:
Warning: Header may not contain more than a single header, new line detected. in C:\wamp\www\ace\dmain.php on line 82.
Line 82 is 'header("Content-length...'
Firefox和Chrome都不会显示任何东西,它们只允许我下载。
以下是代码:
<?php
if (isset($_POST['downloadid'])) {
$fileid = $_POST['downloadid'];
try {
$sql = "SELECT * FROM `datastore` WHERE `id` = '".$fileid."'";
$results = $pdo->query($sql);echo $sql;
while ($row = $results->fetch()) {
$filename = $row['filename'];
$mimetype = $row['mimetype'];
$filedata = $row['filedata'];
header("Content-length: strlen($filedata)");
header("Content-type: $mimetype");
header("Content-disposition: download; filename=$filename"); //disposition of download forces a download
echo $filedata;
// die();
} //of While
} //try
catch (PDOException $e) {
$error = '<br>Database ERROR fetching requested file.';
echo $error;
die();
} //catch
} //isset
?>