首页 > 解决方案 > html5 下载和 php 下载无法在移动设备上运行

问题描述

在我的网站上,我有一个<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);

在移动设备 (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();

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

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

今天的输出如下,但是昨天的content-type说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 )

我还检查了我的 php 信息设置,没有看到任何可能导致问题的东西,但是我又不太确定要寻找什么。

我知道可以在 chrome mobile 上下载文件,因为其他网站适合我,而不是我自己的。

我究竟做错了什么?

标签: phphtmldownloadhttp-headerscontent-type

解决方案


将此添加为标题:'X-Content-Type-Options: nosniff'; 它是为了防止浏览器自己解释接收到的内容。与浏览器相关联Content-disposition: attachment; ...应提供下载它。


推荐阅读