html5下载和php下载在移动设备上不起作用

Ric*_*ich 7 php html5 content-type download http-headers

在我的网站上,我有一个<a download></a>链接以及一个php函数,可以很好地将文件下载到桌面上-

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);
Run Code Online (Sandbox Code Playgroud)

在移动设备(iPhone)上,此功能不适用于Chrome浏览器。(即使我已经看到一些下载提示用户打开另一个应用程序以继续,我也已经知道Safari ios下载。)

因此,我尝试使用<a></a> download链接,但是单击它时,它只是在新标签中打开视频并播放。

如果我尝试使用php脚本,它将在新标签页中打开视频并显示划掉的播放按钮(视频甚至无法播放)。我一直在寻找答案的几天,我已经编辑了.htaccess文件并测试了不同的脚本,内容类型,标头等。

这是特定于移动设备的当前脚本的样子-

... 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();
Run Code Online (Sandbox Code Playgroud)

我还尝试了在移动设备上测试其中一个文件的标题的打印-

print_r(get_headers($url));
print_r(get_headers($url, 1));
Run Code Online (Sandbox Code Playgroud)

今天的输出如下,但是昨天的内容类型说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 )
Run Code Online (Sandbox Code Playgroud)

我还检查了我的PHP信息设置,没有看到可能引起问题的任何内容,但是我还是不太确定要查找的内容。

我知道可以在chrome手机上下载文件,因为其他网站对我有用,而不是我自己的。

我究竟做错了什么?

Bas*_*aar 0

您确定使用的格式是:

<a href="myvideo.mp4" download>Download</a>
Run Code Online (Sandbox Code Playgroud)

正如 miken32 提到的:

“您将MIME 类型设置为video/mp4。如果浏览器可以播放这种视频,它就会播放。将其改回application/octet-stream.

编辑: 您也可以尝试在您的文件中设置它.htaccess

AddType application/octet-stream .mp4
Run Code Online (Sandbox Code Playgroud)

然后将其改回application/octet-stream.

尝试这个。