readfile downloads corrupt files - php

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

Related

How force download an .xlsx file in PHP without having it corrupted?

None of the already existing questions have helped me.
I'm trying to force download an excel file with the file type .xlsx. It works perfectly fine in a download code like this:
echo "<a href='" . $dateiname . "'>Datei herunterladen</a>";
But whenever I try to make a forced download, it doesn't work. I've tried various headers from various questions on stackoverflow, the last two being
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $dateiname . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Pragma: no-cache');
header('Content-Length: ' . filesize($dateiname));
$objWriter->save('php://output');
and
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Transfer-Encoding: binary ");
header('Content-Disposition: attachment; filename='.basename($dateiname));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($dateiname));
$objWriter->save('php://output');
I tried both in the same file as where I create the file, but also in a separate file, but either way I always get the error:
Excel cannot open the file (filename) because the file format or file extension is not valid. Verify that the file has not been corruted and that the file extension matches the format of the file.
The file itself on my server seems perfectly fine.
I've found how I used PHPExcel to force downloading an xlsfile.
header("Content-Disposition: attachment; filename={$fileName}.{$fileFormat}");
header("Content-Type: application/vnd.ms-excel;");
header("Cache-Control: max-age=0");
$objWriter->save('php://output');
Hope it helps.

PHP cant download image with readfile

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)

Force Download with PHP - Server Configuration?

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 Download Issue - Can't Read

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. :)

ModX Evo: PHP readfile() in snippet?

I have a script that downloads a large file (1.3gb) using readfile().
If I create a .php page with just the script on it it works fine, but if I place the same script in a Snippet and place it on a page nothing happens.
Is ModX blocking the download some how? Any advice would be great thanks!
EDIT code:
$file = $_SERVER['DOCUMENT_ROOT']."/movie.mov";
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');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
};
Modx does have a maximum file size setting [Maximum upload size upload_maxsize] but that is for the file manager. I doubt that is your problem.
let's see the script and error logs.
UPDATE
just tested your little snippet out [with a couple of minor changes] ~ it works fine.
$base_path = $modx->config['base_path'];
$movie = 'frankenweenie-mrwhiskers_r640s.mov';
$file = $base_path.$movie;
if (file_exists($file)) {
echo 'file exists '.filesize($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');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
return true;
}else{
echo 'file does not exist';
return false;
}
using the $modx->config['base_path'] is not your problem, it worked using the server vars as well, it's just a good habit. as are the returning true/false modx expects it's snippets to return something whether true, false or $output ... also, not your problem it worked without.
Start looking at your php settings, I think possibly memory limit could be the problem. Check the php docs & see if it needs to have enough memory available to read a file that size. [even though it indicates 'size doesn't matter']
enable error logging in the script itself & check the server error logs.
It works with small files? Then look here: PHP readfile() and large downloads
Good luck!

Categories