【发布时间】:2012-05-25 01:16:14
【问题描述】:
我的上传/下载过程存在一些问题。我的文件作为 BLOB 插入到 mysql 中,但是下载时它会抓取调用 php 页面的 html 标记。在插入数据库之前,我是否需要将上传的文件移动到临时目录?
编辑:当前的 upload.php 文件。仍然获得 html 内容而不是实际文件本身,它仍然被正确命名。
<?php
// Make sure an ID was passed DOWNLOAD HANDLER *******
if(isset($_GET['id'])) {
// Get the ID
$id = intval($_GET['id']); var_dump($id);
require_once ('../mysqli_connect.php'); //Connect to the db
// Fetch the file information
$downloadq = "
SELECT `file_type`, `size`, `title`, 'content', 'upload_id'
FROM `upload`
WHERE `upload_id` =".$id;
$result = mysqli_query ($dbc, $downloadq); // Run the query
if($result) {
// Make sure the result is valid
if (mysqli_num_rows($result) > 0) {
// Get the row
$row = mysqli_fetch_assoc($result);
//var_dump($row);
// Print headers
header("Content-Type: application/msword");
header("Content-Length: ". $row['size']);
header("Content-Disposition: attachment; filename=". $row['title']);
header("Content-Transfer-Encoding: binary");
// Print data
echo (stripslashes($row['content']));
}
else {
echo 'Error! No such ID.';
}
// Free the mysqli resources
mysqli_free_result($result);
}
else {
echo "Error! Query failed: <pre>{$dbc->error}</pre>";
}
mysqli_close($dbc);
}
?>
【问题讨论】:
-
请发布下载代码。如果您使用标题,则页面中不应包含任何文本。
-
“它抓取调用 php 页面的 html 标记”是什么意思? - 你能上传它创建的文件吗?
-
很难解释。假设我在 newpub_profile.php - 显示表格和下载链接的页面。我单击“下载”,下载了一个具有正确标题的文件,但是,当我打开该文件时,我发现该文件的内容是 newpub_profile.php 的 html。所以我可以看到我的页眉、侧边栏、页脚、图像。
-
您应该将页面内容类型设置为该文件特定类型。这可能是一个很好的来源:ryboe.com/tutorials/php-headers-force-download。但我不确定,因为我从来没有任何项目使用数据库 BLOB 格式来恢复我的文件。