html5下载和php下载无法在移动设备上运行

问题描述 投票:7回答:3

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

今天的输出如下,但昨天的内容类型说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上下载文件,因为其他网站对我有用,而不是我自己的。

我究竟做错了什么?

php html5 download http-headers content-type
3个回答
1
投票

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


0
投票

您确定使用的格式是:

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

正如miken32所说:

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

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

AddType application/octet-stream .mp4

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

试试这个。


0
投票

前一段时间尝试制作一个小应用程序和下载标签只是在Chrome中为我工作。像这样:

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

© www.soinside.com 2019 - 2024. All rights reserved.