Force Download with PHP - Server Configuration? - php

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.

Related

readfile downloads corrupt files

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

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!

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.

Why is this PHP download script failing only on Android

I have a simple script that allows someone to download a movie file to their device. The code works well on everything I've tested except for Andriod. The Android device butchers the name and the file extension. It might call the file 2.qt or .bin. Why is this failing?
<?php
if(isset($_GET['filename'])) {
header('Content-Description: File Transfer');
header('Content-Type: movie/quicktime');
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");
} else {
echo "Link: <a href='test.php?filename=test.mov'>Download Video</a>";
}
?>
Because Android doesn't natively support Quicktime, an Apple technology. It's also very possible that the client used to download this app isn't respecting the filename set on the http envelope as the name it uses to write the file to the filesystem, as there is nothing forcing it to.
The problem was that the filenames had spaces, and Android doesn't like that. I removed the spaces and everything is fine.

Categories