Mick Jack
Mick Jack

Reputation: 580

PHP downloading video files from server

User can stream videos from my site. i want to add a button so when user click on the button it will allow the browser to download the file,

How do i do that?

Retrieve video to stream

  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>

Download button

<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;
                    ?>
                    }   

Upvotes: 1

Views: 5763

Answers (5)

Divakarcool
Divakarcool

Reputation: 481

$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);

Upvotes: 0

duduwe
duduwe

Reputation: 1260

I am assuming that your web and database are in one server. Otherwise, implementation would be totally different or may be done through FTP.

html

If you are using bootstrap, you can easily transform anchor tag to button display. I recommend anchor tag over button for download links.

<a href="<?php echo 'http://baseurl/download_video.php?video_id='.$video_arr['video_id']; ?>">Download</a>

php - download_video.php

I recommend GET method over POST, since you will just be fetching data.

// 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;

Upvotes: 0

tRx
tRx

Reputation: 813

As i already mentioned in a comment the download attribute doesn't work well in IE Browsers(tested it by myself! | Also on can i use) .

Instead i would go with something like this:

          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;
                }
            }

EDIT:

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;
                }
            }

So you only need to change the paths i mentioned in the code and get your filename and everything should work fine. I hope i could help you :)

Upvotes: 0

Bhaumik Thakkar
Bhaumik Thakkar

Reputation: 580

Yes you can simply use download attribute of HTML like below

<a href="**YOUR FILE LINK**" download>Click here to Download</a>

This may be help you!

Upvotes: 0

user4454229
user4454229

Reputation:

Add a link tag somewhere, linking to your video file:

<a href='your-video.mp4' download>

Upvotes: 1

Related Questions