Kurrel Posted November 27, 2007 Share Posted November 27, 2007 I am trying to download a generated pdf file from the server to the local machine. However, when running the script : header("Content-type: application/pdf"); header("Content-disposition: attachment; filename=driverpdf.pdf"); header("Content-Length: " . filesize(FIRSTBASE."/images/graphs/driverpdf.pdf")); header("Pragma: no-cache"); header("Expires: 0"); readfile(FIRSTBASE."/images/graphs/driverpdf.pdf"); return; It downloads 'index.php'. Has anyone seen this and do they possibly know what could be going on? I don't understand why it's doing this, and can't find an explanation. Link to comment https://forums.phpfreaks.com/topic/79058-solved-downloading-php-file-rather-than-indicated-pdf-file/ Share on other sites More sharing options...
~n[EO]n~ Posted November 27, 2007 Share Posted November 27, 2007 How are you downloading , through a link or ? I don't see any errors, I have that exact thing and it is working for me... better you keep that in a separate file and a link from where you want to make it download mine is like this download.php <?php $filepath="userdownloads/verso.pdf";; // change your dir path $filename="verso.pdf"; // change your file name downloadFile($filename,$filepath); function downloadFile($filename,$filepath) { header("Content-type: application/pdf"); header("Content-disposition: attachment; filename=".$filename); header("Content-Length: " . filesize($filepath)); header("Pragma: no-cache"); header("Expires: 0"); readfile($filepath); return; } ?> and from the page where i want to download <a href="download.php">Click here to download the pdf</a> Link to comment https://forums.phpfreaks.com/topic/79058-solved-downloading-php-file-rather-than-indicated-pdf-file/#findComment-400112 Share on other sites More sharing options...
Kurrel Posted November 27, 2007 Author Share Posted November 27, 2007 Okay, that works so far as not throwing errors, but the pdf is then empty, containing no data. Double-checking, the original remains working and has all details in it but not the copied version. I wonder if the constant FIRSTBASE and file path are not coming through correctly? <?php $filepath=FIRSTBASE."/images/graphs/driverpdf.pdf"; // change your dir path $filename="driverpdf.pdf"; // change your file name downloadFile($filename,$filepath); function downloadFile($filename,$filepath) { header("Content-type: application/pdf"); header("Content-disposition: attachment; filename=".$filename); header("Content-Length: " . filesize($filepath)); header("Pragma: no-cache"); header("Expires: 0"); readfile($filepath); return; } ?> Also, the new file is named to driverpdf-1.pdf rather than remaining the same as the original. Is this significant? Link to comment https://forums.phpfreaks.com/topic/79058-solved-downloading-php-file-rather-than-indicated-pdf-file/#findComment-400152 Share on other sites More sharing options...
Kurrel Posted November 27, 2007 Author Share Posted November 27, 2007 The issue was that the constant was defined in index.php and did not exist in the new .php file. Fixing that has resolved the issue and it now downloads sweetly. Link to comment https://forums.phpfreaks.com/topic/79058-solved-downloading-php-file-rather-than-indicated-pdf-file/#findComment-400208 Share on other sites More sharing options...
raybanz21 Posted November 13, 2008 Share Posted November 13, 2008 hello sir! I dont know if I am related to this topic. But I have my problem in opening a pdf file. my issue i opposite to this topic. I want to open my pdf link into a new browser not to have an option for downloading it... can you help me with this issue? thanks BonBon Link to comment https://forums.phpfreaks.com/topic/79058-solved-downloading-php-file-rather-than-indicated-pdf-file/#findComment-689035 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.