Jump to content

[SOLVED] Downloading php file rather than indicated pdf file.


Recommended Posts

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.

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>

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?

  • 11 months later...

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.