PHP防止MP4等资源文件被直接下载,资源文件可以通过html中的标签去引用播放,然而用连接直接打开无法下载或者下载下来无法观看
$mp4 = "E:/"."123.mp4"; //页面直接输出视频 $filePath = $mp4; ini_set('memory_limit', '512M'); header("Pragma: public"); header("Expires: 0"); header("Content-Type: application/octet-stream"); //文件mime类型 ob_clean(); flush(); @readfile($filePath);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://code.jquery.com/jquery-1.11.3.js"></script> </head> <body> <video id="video1" controls="controls" style="width: 100%;height: 100%"></video> <script> $(function(){ let xhr = new XMLHttpRequest() xhr.open('GET', 'goVideo.php', true) xhr.responseType = 'blob' xhr.onload = function(e) { if (this.status === 200) { // 获取blob对象 let blob = this.response // 获取blob对象地址,并把值赋给容器 $("#video1").attr("src", URL.createObjectURL(blob)); } } xhr.send() }) </script> </body> </html>
发表评论