Home » Php » html – html5 download and php download not working on mobile devices

html – html5 download and php download not working on mobile devices

Posted by: admin July 12, 2020 Leave a comment

Questions:

On my site I have a <a download></a> link and also a php function that downloads files on my desktop just fine –

ob_start();

$nameOld = 'https://my-other-server.online/path/to/file.mp4';
$save = '/var/path/to/file.mp4';
$nameNew = "download.mp4";

file_put_contents($save, fopen($nameOld, 'r'));

set_time_limit(0);

header('Connection: Keep-Alive');
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=$nameNew");
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($save));

      while (ob_get_level()) {
        ob_end_clean();
      }

readfile($save);
exit;

unlink($save);

On mobile devices (iphone) this doesnt work on chrome browser. (Im already aware about safari ios downloads, even though I’ve seen some downloads prompt the user to open another app to continue.)

So I’ve tried using the <a></a> download link, but when clicking on it, it simply opens the video in a new tab and plays it.

If I try with a php script, it opens the video in a new tab and shows a crossed out play button (video is not even playable). I’ve been searching for an answer for days, I’ve edited .htaccess files and testing different scripts, content-types, headers, etc.

This is how the current script looks like for mobile specific –

... first few lines same as above script ...

file_put_contents($save, fopen($nameOld, 'r'));

//echo file_get_contents($save);
//$headers = get_headers($nameOld, 1);
//$filesize = $headers['Content-Length'];
//set_time_limit(0);

ob_clean();

//if(ini_get('zlib.output_compression'))
               // ini_set('zlib.output_compression', 'Off');
            //$fp = @fopen($save, 'rb');

//header('Connection: Keep-Alive');
//header('Content-Description: File Transfer');
//header('Content-Type: application/force-download');
//header('Content-Type: application/octet-stream');
header('Content-Type: video/mp4');
//header("Content-Disposition: attachment; filename=$nameNew");    
header("Content-disposition: attachment; filename=\"" . basename($save) . "\"");
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($save));
//ob_end_clean();

     while (ob_get_level()) {
        ob_end_clean();
      }

//fpassthru($fp);
  //          fclose($fp);

readfile($save);
//exit;

unlink($save);    
die();

I also tried testing printing on mobile the headers of one of the files –

print_r(get_headers($url));
print_r(get_headers($url, 1));

The output today is as follows, but yesterday the content-type said application/octet-stream

Array ( [0] => HTTP/1.1 200 OK [1] => Date: Wed, 27 Feb 2019 14:51:56 GMT [2] => Server: Apache/2.4.29 (Ubuntu) [3] => Last-Modified: Tue, 26 Feb 2019 14:16:19 GMT [4] => ETag: "3e7e3a-582ccb37e75a7" [5] => Accept-Ranges: bytes [6] => Content-Length: 4095546 [7] => Connection: close [8] => Content-Type: video/mp4 ) Array ( [0] => HTTP/1.1 200 OK [Date] => Wed, 27 Feb 2019 14:51:56 GMT [Server] => Apache/2.4.29 (Ubuntu) [Last-Modified] => Tue, 26 Feb 2019 14:16:19 GMT [ETag] => "3e7e3a-582ccb37e75a7" [Accept-Ranges] => bytes [Content-Length] => 4095546 [Connection] => close [Content-Type] => video/mp4 )

I’ve also checked my php info settings and didnt see anything that could be causing an issue, but then again Im not too sure what to look for.

I know its possible to download files on chrome mobile because other sites work for me, just not my own.

What am I doing wrong?

How to&Answers:

Add this as a header : 'X-Content-Type-Options: nosniff';
it is made to prevent browsers from interpreting themself the received content.
In association wth Content-disposition: attachment; ... the browser should offer to download it.

Answer:

Are you sure you are using the format:

<a href="myvideo.mp4" download>Download</a>

And as miken32 mentioned :

“You’re setting the MIME type to video/mp4. If the browser can play that kind of video, it will do so. Change it back to application/octet-stream.

EDIT:
You could also try to set this in your .htaccess file:

AddType application/octet-stream .mp4

And then change it back to application/octet-stream.

Try this.

Answer:

A while ago trying to make a small application and the download tag just worked for me in Chrome. Like this:

<a href="http://URL" download target="_blank"> Download </a>