使用php从mysql下载路径保存的视频

时间:2014-11-06 21:00:06

标签: php mysql

我使用的是保存在数据库中的目录路径。我怎么能从路径下载。我的代码是。

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

?> 

1 个答案:

答案 0 :(得分:1)

设置下载标题后,需要输出/回显文件内容。

还有以下

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

应该是

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

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

完整代码(我假设您有一个名为video_path的列,用于存储服务器上文件的路径:

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']);
}

最后评论后的新代码版本:

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