Home  >  Article  >  Backend Development  >  javascript - How to realize that when the user opens the video address, it does not play but downloads

javascript - How to realize that when the user opens the video address, it does not play but downloads

WBOY
WBOYOriginal
2016-08-04 09:19:202390browse

How to make the user download the video instead of playing it when opening the video address

Reply content:

How to make the user download the video instead of playing it when opening the video address

Use PHP to proxy downloads

<code><?php
    $file = "/tmp/视频.mp4";
    $filename = basename($file);
    header("Content-type: application/octet-stream");
    //处理中文文件名
    $ua = $_SERVER["HTTP_USER_AGENT"];
    $encoded_filename = rawurlencode($filename);
    if (preg_match("/MSIE/", $ua)) {
     header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
    } else if (preg_match("/Firefox/", $ua)) {
     header("Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"');
    } else {
     header('Content-Disposition: attachment; filename="' . $filename . '"');
    }
    header("Content-Length: ". filesize($file));
    readfile($file);</code>

Quoted Brother Bird’s code: http://www.laruence.com/2012/...

a Tag H5 has a new attribute download, which allows the browser to download the file connection by default and specify the download file name. However, there are compatibility issues, you can try
Link description

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn