Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 download and php download not working on mobile devices

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?

like image 585
730wavy Avatar asked Feb 27 '19 15:02

730wavy


1 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.

like image 127
Tuckbros Avatar answered Nov 19 '22 17:11

Tuckbros