Download a pdf's file code in php

13,552

Solution 1

BASE_URL probably has a slash at the end, so you don't need the extra one:

$path = BASE_URL."pdf/"; 

Solution 2

I don't see where you ever set the value of $filename, you set the location of the PDF in $fullPath, but then use $filename to read it out. I think the code should be

$path = BASE_URL."/pdf/"; 
$fullPath = $path.basename($_GET['download_file']);

header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; 
filename='.basename($fullPath));
header("Content-Type: application/pdf");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($fullPath));
readfile($fullPath);
exit;
Share:
13,552
Subek Shakya
Author by

Subek Shakya

Management Professional with proven experience in Information Technology. Have experience in both software development and management. Expertise : Strategy and Operations , Project Management , Dot Net Technologies, Database Programming , PHP, Open Source, System Architect and Design , Team management , Client Management etc.

Updated on June 16, 2022

Comments

  • Subek Shakya
    Subek Shakya almost 2 years
    $path = BASE_URL."/pdf/"; 
    $filename= $path.basename($_GET['download_file']);
    
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header('Content-disposition: attachment; 
    filename='.basename($filename));
    header("Content-Type: application/pdf");
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: '. filesize($filename));
    readfile($filename);
    exit;
    

    This code works but I'm getting Error in reading pdf file when opening the downloaded pdf. In the above code I get the file from the location http://localhost//eec//pdf/CV_Prabin Mishra.pdf