user3071331
user3071331

Reputation: 23

download video from path save in mysql using php

I am using directory path that i save in database. how can i download from path. my code is.

        if(isset($_POST['download'])) {

        $id = $_GET['id'];
        $query = "SELECT url_video FROM website WHERE id = '$id'"; 
        $result = mysql_query($query); $rowss = mysql_fetch_assoc($query); 
        $name2 = $rowss['url_video'];

        header("Accept-Ranges: bytes"); 
        header("Keep-Alive: timeout=15, max=100");
        header('Content-Disposition: attachment; filename='.$name2);
        header("Content-type: video/mp4"); 
              //header("Content-Transfer-Encoding: binary");   
        header( "Content-Description: File Transfer");

        exit;        
    }

?> 

Upvotes: 0

Views: 719

Answers (1)

AlexL
AlexL

Reputation: 1707

You need to output/echo the content of the file after you set the download headers.

Also the following

header('Content-Disposition: attachment; filename='.$name2);

should be

header('Content-Disposition: attachment; filename='.$file_name_not_full_path);

or

header('Content-Disposition: attachment; filename=downloadedvideo.mpg');

Full code (i assume you have a column named video_path that stores the path to the file on the server:

if(isset($_POST['download'])) {

    $id = $_GET['id']; 
    query = "SELECT url_video, video_path FROM website WHERE id = '$id'"; 
    $result = mysql_query($query); 
    $rowss = mysql_fetch_assoc($query); 

    header("Accept-Ranges: bytes"); 
    header("Keep-Alive: timeout=15, max=100"); 
    header('Content-Disposition: attachment; filename='.$rowss['url_video']);
    header("Content-type: video/mp4"); 
    header("Content-Transfer-Encoding: binary"); 
    header( "Content-Description: File Transfer");

    readfile($rowss['video_path']);
}

New code version after last comments:

if(isset($_POST['download'])) {

    $id = $_GET['id']; 
    query = "SELECT url_video FROM website WHERE id = '$id'"; 
    $result = mysql_query($query); 
    $rowss = mysql_fetch_assoc($query); 

    header("Location: ".$rowss['url_video']);
    exit();
}

Upvotes: 1

Related Questions