I have a project in which I desire to upload and download blob files in php. I have written the codes to upload the files into mysql however, when I try to download the files, be it pdf, .doc or jpeg, the error message I get is “The selected file can not be opened”. I need assistance in this regard pls. The download codes are:
<?php
if(isset($_GET['download']))
{
// if id is set then get the file with the id from database
include 'open_db.inc';
$name = $_GET['download'];
$query = "SELECT name, type, size, content " .
"FROM upload WHERE name = '$name'";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);
header("Cache-Control: maxage=1"); //In seconds
header("Pragma: public");
header("Content-length:$size");
header("Content-type:$type");
header("Content-type:application/pdf");
header("Content-Disposition: attachment;filename=\\"$name\\"");
header("Content-Description: PHP Generated Data");
header("Content-transfer-encoding: binary");
ob_end_clean();
echo $size;
echo $type;
$content = stripslashes ($content);
echo $content;
exit;
}
?>
I don’t see how you could get that error. Try the script below and see if it works with content from the regular file. If it is ok then I would check the database query and make sure the content is valid.
Try opening it up in notepad++ or another lightweight text editor. Is there anything unexpected at the beginning of the file? Like… for example… the size and type shouldn’t be in the actual file output;
Also note that you should check that the database record actually exists first.
Try the following:
<?php
if(isset($_GET['download']))
{
// if id is set then get the file with the id from database
include 'open_db.inc';
$name = $_GET['download'];
$query = "SELECT name, type, size, content " .
"FROM upload WHERE name = '$name'";
$result = mysql_query($query) or die('Error, query failed');
if($row = mysql_fetch_array($result)){
list($name, $type, $size, $content) = $row;
ob_end_clean();
header("Cache-Control: maxage=1"); //In seconds
header("Pragma: public");
header("Content-length:$size");
header("Content-type:$type");
header("Content-type:application/pdf");
header("Content-Disposition: attachment;filename=\\"$name\\"");
header("Content-Description: PHP Generated Data");
header("Content-transfer-encoding: binary");
echo stripslashes($content);
exit;
}else{
echo "File does not exist";
}
}else{
echo "No download selected";
}
?>
I have tried to open the file in notepad and I get this
347567application/pdf%PDF-1.4
%âãÏÓ
1 0 obj
<</CreationDate (D:20120601151956)/ModDate ()/Producer (PDF Complete version 3.5.1.1)/Title (Microsoft Word - JMPR-10-176 Harun et al)>>
with some other corrupted documents.
I tried working with the codes you gave me, but the pdf downloads but has nothing on it, the message still remains “The selected file can not be opened”. I am still open for more suggestions. Thanks once again.
Thanks tom8. I have tried using your codes but no improvement. When I checked my DB, I find that the file had been uploaded. Funny enough, when I download, it gives me the exact size that was uploaded into the DB but it comes blank with the message I mentioned earlier. I am still open to further suggestions.
Thanks so much Jake. I have resolved it with these codes:
<?php
if(isset($_GET[‘download’]))
{
// if id is set then get the file with the id from database
include ‘open_db.inc’;
//include ‘library/opendb.php’;
$name = $_GET[‘download’];
$query = “SELECT name, type, size, content " .
“FROM upload WHERE name = ‘$name’”;
$result = mysql_query($query) or die(‘Error, query failed’);
list($name, $type, $size, $content) = mysql_fetch_array($result);
ob_end_clean();
header(“Cache-Control: maxage=1”); //In seconds
header(“Pragma: public”);
header(“Content-length:$size”);
header(“Content-type:$type”);
header(“Content-type:application/pdf”);
header(“Content-Disposition: attachment;filename=\”$name\”");
header(“Content-Description: PHP Generated Data”);
header(“Content-transfer-encoding: binary”);
ob_end_clean();
//$content = stripslashes ($content);
echo $content;
//while (@ob_end_clean());
//include ‘library/closedb.php’;
exit;
}else{
echo “File does not exist”;
}
?>
It was suggested by a friend that the two echos preceding echo $content should be removed. I did that and it working well. Thanks for your assistance. I am excedingly grateful.