Forced download stopped working - php

I have a php script that I have used for years to force downloads from my website. But sometime in the last month or so, it stopped working and is triggering file not found errors. The weird thing is that in firefox, if I do view source on the error page, it is the file I was trying to download. And doing File > Save from there give you the correct file. So I know it's not a problem with the script not finding the file on the server.
Is there something wrong with how I am setting up the headers?
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-length: '.filesize($file_url));
header('Content-disposition: attachment; filename="'.basename($file_url).'"');
readfile($file_url);

Can you try this function?
function force_download($file){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}

I ended up cheating to make this work.
header("Location: $file_url"); //file_url is now the real url, not the path
And then used cPanel to make sure all the MIME types I was using were set to application/octet-stream.

Related

corrupted file by saving with CodeIgniter and header output

I want to download a file from the upload folder by calling mydomein/download/filenem
It works in this way, that I received the wanted image, but I think I have a bug in the header which I output into my controller with this code:
$file=my_path_tofile;
header('Expires: 0');
header('Content-Description: File Transfer');
header("Content-type: ".$finfo->file($file));
//header("Content-type: application/".$ext);
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="'.$data['realname'].'"');
header('Cache-Control: must-revalidate');
header("Content-Length: " . filesize($file));
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($file);
exit;
If I call the URL, the download will start, the filename is fine and the size looks right too. But if I want to open the file I get the message that the file could not open in cases of corruption.
I'm not an expert about headers, so can someone explain me what is the problem is and how I can solve it?

Save file under predefined name

I created a File-Download Script that works quite well. There is just one problem: The filename doesn't match the one defined in the header. My code is:
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: filename=\"".$result['filename']."\""); #Here's the problem
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: private');
header('Content-Length: '.$result['size']);
but the file wont download as whatever $result['filename'] is set to but the script name instead.
After renaming the .php to the correct extension it works fine but thats not convenient.

CSV file doesnot downloaded from server in php

I am new to php and have php code which download record from server.
It works fine on my local server but does not work at server side.
at server side it redirect me to controller but in local in works fine.
To download the csv file i have created a new controller file.
here is my code
through this javascript code I am calling my controller
function exportcustomer()
{
window.location=site_url+'controllers/ajax_controller/user-export-controller.php';
}`
and here are the headers
$filename = "UserReport.csv";
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header('Pragma: public'); // required
header('Content-Length: '.filesize($filename)); // provide file size
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header("Content-Type: application/download");
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: binary ");
header("Content-Type: application/octet-stream");
readfile($filename); // push it out
echo $output;
exit;
Do we need to restart server after adding any new controller.
please let me know what is the mistake in this code.
Thanks in Advance

PHP - What are the correct headers for downloading file?

I have a piece of code that allow users download file from server (document such as docs,docx,pdf etc).
Users can download files but it has some errors like the files were broken. For example, a MS Word file after download need to recovery to read content.
I wonder that if there is any mistake in this code (or problem when uploading?).
$size_of_file = filesize($download_path);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $file_name);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size_of_file);
//read file from physical path
readfile($download_path);
Did you try like this ?
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
I found the root of the problem, I hav some extra spaces after php close tag. Thank you guys.

No progress bar in IE8 while executing the download using PHP

I am giving downloads to user using the PHP code below but when user downloading they are not able to see the progressbar in IE8 when clicked on save button. Please solve this.
Thanks in advance.
header('Content-Description: Songsbin.com - Downlaod');
header('Content-type: audio/mpeg');
header('Content-Disposition: attachment; filename='.$filename1);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: '.filesize($file));
ob_clean();
flush();
readfile($file);
Your code is correct, I also tried and both in Chrome and IE8 it works good...
if you succeed to download the file check that filesize($file) returns a correct output..
remove the 'Expires: 0' header directive

Categories