用户可以从我的网站流式传输视频。我想添加一个按钮,所以当用户点击按钮时,它将允许浏览器下载文件,
我该怎么做?
检索要流式传输的视频
if(isset($_POST['video_id'])){
$id = trim($_POST['video_id']);
//check if it exists
$result = mysqli_query($dbc , "SELECT `video_id`, `video_link` FROM `viewvideo` WHERE `video_id`='".$video_id."'");
$count = mysqli_num_rows($result);
//does it exist?
if($count>0){
//exists, so fetch it in an associative array
$video_arr = mysqli_fetch_assoc($result);
//this way you can use the column names to call out its values.
//If you want the link to the video to embed it;
//echo "Video Link: ".$video_arr['video_link'];
echo $video_arr['video_link'];
?>
<div id="video">
<video width="640" height="480" controls>
<source src="<?php echo $video_arr['video_link']; ?>" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
下载按钮
<button id="download" onclick="download()">download</button>
function download() {
<?php
if(isset($_POST['video_id'])){
// get the video link...
$fileName=$video_arr['video_link'];
$fileName=str_replace("..",".",$fileName); //required. if somebody is trying parent folder files
$file = "../folder/".$fileName;
if (file_exists($file)) {
$mime = 'application/force-download';
header('Content-Type: '.$mime);
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$fileName);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
?>
}
答案 0 :(得分:0)
在某处添加链接标记,链接到您的视频文件:
<a href='your-video.mp4' download>
答案 1 :(得分:0)
是的,您只需使用HTML的下载属性,如下所示
<a href="**YOUR FILE LINK**" download>Click here to Download</a>
这可能对你有帮助!
答案 2 :(得分:0)
正如我在评论中已经提到的,下载属性在IE浏览器中不起作用(我自己测试了!| Also on can i use)。
相反,我会选择这样的东西:
if(isset($_POST['video_id'])){
// get the video link...
$fileName=$video_arr['video_link'];
$fileName=str_replace("..",".",$fileName); //required. if somebody is trying parent folder files
$file = "../destinationOfYourFile/".$fileName;
if (file_exists($file)) {
$mime = 'application/force-download';
header('Content-Type: '.$mime);
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$fileName);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
修改强>
HTML:
<button id="download" onclick="download()">download</button>
JS:
function download(){
var fileName = FILENAME; // you need to get your filename, e.g. "video.mp4"
window.location.href = 'https://www.YOURSITE.com/LOCATION/TO/download.php?filename=' + fileName;
}
的download.php:
if(isset($_GET['filename'])){
$fileName=$_GET['filename'];
$fileName=str_replace("..",".",$fileName); //required. if somebody is trying parent folder files
$file = "../destinationOfYourFile/".$fileName; // you need to change the destination to your video folder
if (file_exists($file)) {
$mime = 'application/force-download';
header('Content-Type: '.$mime);
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.$fileName);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
所以你只需要更改我在代码中提到的路径并获取你的文件名,一切都应该正常工作。我希望我可以帮助你:)。
答案 3 :(得分:0)
我假设您的网络和数据库位于一台服务器中。否则,实施将完全不同,或者可以通过FTP完成。
<强> HTML 强>
如果您使用的是bootstrap,则可以轻松地将锚标记转换为按钮显示。我建议在下载链接的按钮上使用锚标记。
<a href="<?php echo 'http://baseurl/download_video.php?video_id='.$video_arr['video_id']; ?>">Download</a>
php - download_video.php
我推荐使用GET方法而不是POST,因为你只需要获取数据。
// query video info here using $_GET['video_id']
$filepath = $result['video_path'];
$filename = base_name($filepath);
// enable php_fileinfo.dll/.so in php.ini as necessary
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $filepath);
finfo_close($finfo);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: ".$mime);
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$fname."\"");
ob_clean();
flush();
readfile($filepath);
exit;
答案 4 :(得分:0)
$file_url = $_GET['link']; // http://www.somedomain.com/test.mp4
header('Content-Type: video/mp4');
header("Content-disposition: attachment; filename=\"" .
basename($file_url) . "\"");
readfile($file_url);