M
M
maxdart2017-09-20 17:14:34
PHP
maxdart, 2017-09-20 17:14:34

How to solve the problem with downloading files from the server in IOS and Android?

Hello!
Faced the following problem. There is a PHP script that takes a file (BLOB, image, or PDF) from the database and issues it to the browser for download, having previously formed the necessary headers. This script works correctly in all tested desktop browsers (FF, Chrome, Opera, MSIE), but when trying to download a file on a mobile device, an empty image is loaded. This pattern is observed, at least in Safari on Iphone (IOS 10.3), and Chrome on devices with Android 6. Various options for sending headers and their combinations have been tried, but so far, alas, to no avail.
I would be grateful for advice on the formation of headings that will be correctly perceived by both desktop and mobile browsers.
Here is a script fragment that is significant in this problem:

$res = $db->getBLOB($_GET['fid']);
$theUA = strtolower($_SERVER['HTTP_USER_AGENT']);
$fileName = $res['name'];
$formatRFC2231 = 'Content-Disposition: attachment; filename*=UTF-8\'\'%s;';
$formatDef = 'Content-Disposition: attachment; filename="%s";';
$format = $formatDef;
if(strpos($theUA,'msie') || strpos($theUA,'chrome')) {
  $fileName = rawurlencode($fileName);
}
if(strpos($theUA,'opera') || strpos($theUA,'firefox')) {
  $fileName = rawurlencode($fileName);
  $format = $formatRFC2231;
}
$contentDisp = sprintf($format, $fileName);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header("Content-type: ".$res['ctype']);
header($contentDisp);
header("Content-Encoding: binary");
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
echo $res['content'];

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question