我试图从我的数据库下载文件,其数据存储为blob。但是,我的代码不断返回html文件而不是实际文件。
更糟糕的是,输出的名称是' handleDownload'这是我的php下载脚本的名称,而不是实际的文件名!
我该如何解决这个问题?提前谢谢!
这是我一直关注的教程:http://www.w3programmers.com/file-upload-and-download-with-php/
下载代码
<?php
$Download = new Download();
$result = array(); //store results of output
try {
//if id is set then get file from database
if(isset($_GET['id'])){
$id = $_GET['id'];
//pump id in to function getDBFiles to pull file with matching id
$Download->getDBFiles($id);
//get array containing file details from function getDBFiles
$getFiles = $Download->getMessages();
$size = $getFiles['size'];
$type = $getFiles['type'];
$name = $getFiles['name'];
$content = $getFiles['content'];
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content; exit;
}
//execute retrieval of files from database
$Download->showDBFiles();
//pass results to output array
$output = $Download->getMessages();
//var_dump($output);
} catch (Exception $e) {
$result[] = $e->getMessage();
}
?>
数据库代码
protected $messages = array();
public function showDBFiles() {
global $database;
$sql = "SELECT resume_id, resume_title FROM ".self::$table_name;
$result = $database->query($sql);
if(mysqli_num_rows($result)==0){
$this->messages[] = "Database is empty";
}else{
while(list($id, $name) = mysqli_fetch_array($result)){
$this->messages[] = array('id'=>$id, 'name'=>$name);
}
}
}
public function getDBFiles($id) {
global $database;
$sql = "SELECT resume_title, file_type, file_size,resume_data FROM ".self::$table_name." WHERE resume_id = $id";
$result = $database->query($sql);
list($name, $type, $size, $content) = mysqli_fetch_array($result);
$this->messages[] = array('name'=>$name, 'type'=>$type, 'size'=>$size, 'content'=>$content);
}
public function getMessages()
{
return $this->messages;
}