I'm trying to download a file from a database but my file is always empty. My vardump output shown below indicates that my data is correctly uploaded but when I use the header to download, I get a html file whose name is that of my download script instead of my intended file and its respective name. Please help me to point out my mistake.
This is my download script:
$Download = new Download();
$result = array(); //store results of output
$row = array();
try {
//execute retrieval of files from database
$Download-> showDBFiles();
//pass results to output array
$output = $Download->getMessages();
//var_dump($output);
//if id is set then get file from database
if (isset($_GET['id'])) {
$id = $_GET['id'];
//pump id into function getDBFiles to pull file with matching id
$Download->getDBFiles($id);
$row = $Download->getMessages();
$fileName = $row[0]['name'];
$fileType = $row[1]['type'];
$fileContent = $row[2]['content']; //content of file
$fileSize = $row[3]['size']; //file size
header('Content-Type:"' . $type . '"');
header('Content-length:"' . $size . '"');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
echo $content;
//var_dump($row);
exit;
}
} catch (Exception $e) {
$result[] = $e->getMessages();
}
This where the functions are called:
protected $messages = array();
public function showDBFiles() {
global $database;
//$sql = "SELECT resume_id, resume_title FROM ".self::$table_name." ORDER BY resume_id";
$sql = "SELECT id, name FROM ".self::$table_name." ORDER BY id";
$result = $database->query($sql);
if(mysqli_num_rows($result)==0){
$this->messages[] = "Database is empty";
}else{
while(list($id, $name) = mysqli_fetch_array($result, MYSQLI_BOTH)){
$this->messages[] = array('id'=>$id, 'name'=>$name);
}
}
}
public function getDBFiles($id) {
global $database;
$sql = "SELECT * FROM ".self::$table_name." WHERE id = $id";
$result = $database->query($sql);
if ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$name = $row['name'];
$type = $row['type'];
$content = $row['content']; //content of file
$size = $row['size']; //file size
$this->messages[] = array('name'=>$name, 'type'=>$type, 'content'=>$content, 'size'=>$size);
}
}
public function getMessages(){
return $this->messages;
}
This is my vardump output:
array(1) { [0]=> array(4) { ["name"]=> string(14) "testing456.doc" ["type"]=> string(18) "application/msword" ["content"]=> string(86016) "...