Pdf error: the selected file can not be opened

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.

<?php
   if(isset($_GET['download']))
   {
	   $name = $_GET['download'];
	   $type = filetype ($name);
	   $content = file_get_contents($name);
	   $size = strlen($content);
	   if(!$size)
	   {
		   echo "File $name contains no data!";
		   exit;
	   }
	   header("Content-Length:$size");
	   header("Content-type:$type");
	   header("Content-Disposition: attachment;filename=$name");
	   echo $content;
	   exit;
   }
   echo "No file specified!";
?>

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";
}
?>

Thanks Arkinstall.

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.

With my code, what does notepad output as the file?

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.

%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)>>

That is what I get with your code with the corrupted file below this. Thanks for your assistance.

Ok, what’s at the very bottom of the file?

Note that the general format should be:

%PDF-1.3
%Äåòåë§ó*ÐÄÆ
4 0 obj
<<... stuff ...>>
{Some ugly code}
%%EOF


With that empty line after %%EOF present

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.