views:

48

answers:

3

Hello,

I'm currently building a script that will allow a user to download a file via a URL without actually seeing the filename or where the file is stored. So far I have everything built out, but I need to know how I would go about calling the file to open and download. I currently have a working version (code below), but for some reason the PHP is corrupting the download. Everytime I try to open a file that downloads to my desktop I get a corrupt error message. When I open the same file on the server itself, the file works just fine.

URL Structure:
http://www.example.com/download/file/cjVQv0ng0zr2

Code that initiates the download

    $fullpath = BASE_PATH . '../uploads/brochures/' . $vendors['0']['filename'];

    header("Content-type: application/pdf");
    header('Content-disposition: attachment; filename="' . $fullpath . '"');

Am I doing something wrong that would cause the file to become corrupt? Am I missing a header or two?

Thanks in advance, Jake

+2  A: 

You need to call the following line after sending the header.

readfile($fullpath); 

and also adjust in the header like this:

header('Content-disposition: attachment; filename="' . basename($fullpath) . '"');

One thing i am not sure about is the $fullpath .. try to see if the $fullpath you have is correct and you can actually reach the file, this needs to be the full physical path of the file.

I think it would also be a good idea to add the following header as well:

header("Content-Transfer-Encoding: binary"); 
Sabeen Malik
Thanks for responding so quick. I have changed the script around to include what you have mentioned, but I still receive the following error: There was an error opening this document. The file is damaged and could not be repaired.
Jake
just to make sure your physical path is correct , just wrap around the code inside if(is_file($fullpath)){}
Sabeen Malik
I see what you are saying about the file path, I will check and make sure, but last time I checked, I believe it was correct.
Jake
I also put in another header line in the answer, what is the file size of the downloaded files?
Sabeen Malik
Okay, I added the other header and still the same issue. Also, I checked to make sure the path was correct and did the if(is_file($fullpath)) and still the same problem. The file size of the PDF is approx. 25.2 KB (very small)
Jake
have u tried opening the downloaded file with notepad? see if you see anything wrong at the very top or very bottom. Like Brett says it could be an empty line issue.
Sabeen Malik
Yes, I have looked in notepad and see some very weird data being output at the top of the page. It's all gibberish and weird characters.
Jake
do you wanna IM?
Sabeen Malik
A: 

I had a similar issue a while back. Make sure you don't have any extra whitespace in your script file, either before the "<?php" tag or after the "?>" tag. In my case the last character of my script was "\n" instead of the expected ">".

Brett
A: 

I had faced the same problem sometime back, following worked for me; put a

while( @ob_end_clean() );

just before header functions:

header("Content-Type: ". $row['p_mime']);
header("Content-Length: ". $row['p_size']);
header("Content-Disposition: inline; filename=".$row["p_name"]);

Content-disposition: attachment/inline has to be set according to cases (1. prompt for download / 2. open in browser)

NOTE: Take care that you are not echoing and value before the header function, and being over cautious will not do any harm, silent out all the function before header function which you think would fail or spawn a warning message prefixing "@" symbol to those lines of php code.

all the best :)

Idlecool
somebody! enlight me! why i am down-voted?
Idlecool