1

I am using mongodb with php. I have agreement download option on which when user clicks user get the agreemment downloaded. On click i have navigated to script where file download code in php is there code is

$mongoDbA = $mongoDb->findOne(array("agency_id" => new MongoId($_GET['id'])));
$filename = '../../images/upload/'.$mongoDbA['file'];

if (file_exists($filename)) 
{
header('Content-Description: File Transfer');
header('Content-Type: text/pdf');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
}

I have allowed to upload only pdf files so only pdf files are there in database

When i click then it gives me error & gives me white colored file that has no extension & size is 1 kb hardly....what is happening???

Stennie
  • 63,885
  • 14
  • 149
  • 175
Shaggie
  • 1,799
  • 7
  • 34
  • 63
  • Please check php error_log. The file you download is empty due to php error. Have you tried opening "white colored" file with notebook? –  Dec 27 '14 at 10:32
  • no. i tried renaming with .pdf the white file then it shows "cannot open file"... – Shaggie Dec 27 '14 at 10:38
  • Open it with notepad if it is a php error it probably is thrown in there too. –  Dec 27 '14 at 10:39

1 Answers1

1
$filename = './images/upload/'.$mongoDbA['file'];

    $fileinfo = pathinfo($filename);
    $sendname = $fileinfo['filename'] . '.' . $fileinfo['extension'];

    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="$sendname"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($filename));
    readfile($filename);
    exit;