This is my code:
$file = 'test.jpg';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
test.jpg is a file on my system. I can open it and nothing is wrong with it. The download gets started but when i try to open the downloaded image, it says that it cant be opened. Im testing it on Chrome. Does someone have a solution? Thanks a lot.
Edit: the downloaded .jpg is only 23kb where as the file i want is 135kb.
Try to set header
header('Content-Type: image/jpeg');
ob_clean();
exit;
try replacing readfile with echo file_get_contents($file)
Related
I may be missing something exceptionally obvious here, but I'm using yii2-flysystem along with Dropbox to read and write files.
I can upload and write them to Dropbox with no problem but then, when reading like this:
$file = Yii::$app->dropboxFs->read($fn);
..all that gives me is a string (/tmp/phpQkg8mJ).
How do I actually force the download of the file that I'm reading? I'm not sure what that temporary file location actually relates to.
Try function readfile().
According to example, your code should be looks something like this:
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
I am facing a problem when i am trying to download a certain file from my localhost. Everytime i download a file, either a .docx, a .pdf or a .png it is always corrupt. I have spend multiple hours on finding a solution but nothing seems to work.
This is the script im running :
$file_download="loonadministratie/{$loon_id}/{$file}";
if (file_exists($file_download)) {
header('Content-Description: File Transfer');
header("Content-Type: application/octet-stream/loonadministratie/{$loon_id}/");
header('Content-Disposition: attachment; filename='.basename($file_download));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file_download));
readfile($file_download);
exit;
}
Thanks in forehand
Try
<?php
if (ob_get_level()) ob_end_clean();
?>
It may be the issue
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'm attempting to force a download of an image that is in a directory above my website root. The download happens ok, and the correct filename is saved. However, the end-file is not a valid image and will not open or display properly. Here's my code:
$photograph = new ViewPhotograph($photograph_id);
$photograph->setPhotographVars();
$file = $photograph->getPath('small');
$filename = '1.jpg';
header('Content-Description: File Transfer');
header('Content-Type: image/jpg');
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($file));
ob_clean();
flush();
readfile($file);
exit;
do a ob_start before calling ob_clean()
once something is outputed by using echo or similar, you can't get rid of this, except, when you start a buffer before
Perhaps there's a PHP warning corrupting the stream.
Comment out the headers and see if you see a warning. If you do, fix it. (note that you shouldn't have display_errors turned on production servers).
When I download the original zip it works fine, but when I download it using the below headers and stuff it doesn't work. I know it's better to take this route and tell the browser how to handle the file rather than leave it up to the browser, but I can't get this to work, so I'm tempted to use a header() forward.
$path = $this->tru->config->get('root.path').'/Digital Version of Book for Web.zip';
set_time_limit(0);
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-Type: application/zip');
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename="NewFileName.zip"');
header('Content-Length: ' . filesize($path));
$f = fopen($path, 'rb');
fpassthru($f);
fclose($f);
Edit:
Sorry, what I mean by it doesn't work is that the file downloads in a zip format (all 9.3 MB) but I'm unable to unpackage the zip because it's invalid.
Take a look into the ZIP file using Notepad or another text editor. Check whether there is a PHP error message screwing up the file on the first few lines. It could be a "headers already sent" message or the set_time_limit() call throwing an error due to the script being in safe mode.
Try using readfile(). An example is provided in the PHP Manual.
$file = 'monkey.gif';
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;
}