0

I have to download most recent uploaded PDF file from MySQL database using PHP. The file can view but while saving it to local folder, instead of saving it as .pdf , it saves .php. and that .php file contains encoded data.

Can anyone suggest how I download/save .pdf file? Code is:

<?php 
include 'connection.php';

  $sql=mysqli_query($connection,"Select name,content from ekalp where id = (select max(id) from ekalp)"); 
$result=mysqli_fetch_assoc($sql);
//$resu=$result['name']; 
$result=$result['content'];
echo $result."<br>";
$filename = $result.'pdf';
  header('Content-type: application/pdf');
  header('Content-Disposition: inline; filename="' . $filename . '"');
  header('Content-Transfer-Encoding: binary');
  header('Accept-Ranges: bytes');
  ob_clean();
  ob_flush ();
  @readfile($filename);
 mysqli_close($connection);

?>
halfer
  • 19,824
  • 17
  • 99
  • 186
user3766182
  • 15
  • 1
  • 3
  • 9

2 Answers2

1

See this solution, reproduced here:

Adding ob_clean(); and flush(); functions before the readfile(); function, could be something worth using, as per what the PHP manual states on the subject.

readfile() http://php.net/manual/en/function.readfile.php

ob_clean() http://php.net/manual/en/function.ob-clean.php

flush() http://php.net/manual/en/function.ob-flush.php

These functions are not present in your posted code

Community
  • 1
  • 1
Parthi04
  • 1,121
  • 4
  • 21
  • 39
0

I am assuming $resu holds a uploaded file name. Then why you just don't link to file ??

<?php 
include 'connection.php';

$sql=mysqli_query($connection,"Select name,content from ekalp where id = (select max(id) from ekalp)"); 
$result=mysqli_fetch_assoc($sql);
$resu=$result['name'];
?>

<a href="http://YOUR UPLOAD FILE PATH/.<?php echo $resu?>.pdf">Download File </a>
CaPs LoCk
  • 150
  • 4