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
Related
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?
I've searched alot for that and nothing works for me. The Problem is that the file got displayed in the Browser. It includes JSON, but is an .locx file (needed for an APP). No not my APP so i cant change that. I've tested so much variations of Content Types and Encodings, but nothing helped me out. That is my Code.
<?php
function DownloadFile($file) {
if(file_exists($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));
ob_clean();
flush();
readfile($file);
exit;
}
}
DownloadFile('storage/3-3-2.locx');
?>
Thanks for reading!
// UPDATE
It works on another Server, and as written in the comments the Script works very well, but what i have to change in the server configuration that it works for me too?
It happens , while uploading the file.
While uploading the file encoding changed from utf8 to iso.
<?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 have the following code for downloading files automatically
at the click of a submit button, everything seems to work fine;
the file downloads in the rigth format, right size, right name, but when I
want to open it, I get an error, the file cannot be read, what could be the
problem?
$file=mysql_fetch_assoc($sel);
$file=$file['downloadlink'];
header('Content-Type: "application/octet-stream"');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment; filename=\"".basename($file)."\"");
readfile($file);
you could try tweaking this function from the readfile() comments:
function DownloadFile($file) { // $file = include path
if(file_exists($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));
ob_clean();
flush();
readfile($file);
exit;
}
}
I have an addition for this. If the file size is very big, it'll download empty file which we cannot open at all. That is not a problem with 'readfile' function itself. The problem is reading large files into memory. So, for preventing that kind of issues, we have to use 'ob_end_flush()' immediately before to the 'readfile' function for turning off output buffer.
Hope this tip will save someones time. :)
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.