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?
Related
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
$file = SEVENCE_DATA_ROOT."/resource/devdemo/promo_card.tiff";
header('Content-Description: File Transfer');
header('Content-type: application/tiff');
header("Content-Type: application/force-download");// some browsers need this
header("Content-Disposition: attachment; filename=promo_card.tiff");
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);
exit;
?>
My issue is that the download image can't be viewed. Can anyone help me with this issue?
Content-Type: application/force-download");// some browsers need this
No browser does need this. It's a dirty glitch..
Reasons why your file cannot be opened:
You have a space or invisible characters at the beginning of your script
the path you are loading the file from might be wrong and it doesn't even read from it (what size is the downloaded file?)
the file simply is corrupt
you don't have a tiff-viewer
The correct mime type is image/tiff (instead of application/tiff)
I save many documents outside the webroot.
I want to click a link, that opens a new window (target="_blank"), and force download the file that's found.
Here's what I've got so far, but my results show gobble-de-gook in the browser popup, rather than forcing the download to the desktop:
function download($filelocation){
$filename = basename($filelocation);
if (file_exists($filelocation)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
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($filelocation));
ob_clean();
flush();
readfile($filelocation);
exit;
}
}
In the new browser window I simply call that download() function with a specific path the the file.
It's definitely finding the file, but now I'm just wondering what I'm missing with header() to force the file through the browser.
Missing this:
header("Content-Type: application/force-download");
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.
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.