如果“extension”行等于mp4,则显示视频播放器

时间:2014-11-01 18:21:14

标签: php mysql

当用户下载文件时,我想允许流式传输视频文件而不是自动下载。我已经尝试过使用if语句,所以当文件数据库中的扩展行是MP4时,而不是下载它会显示视频播放器,不幸的是我收到错误"不能使用PDOStatement类型的对象作为数组" ,我想知道解决这个问题的最佳方法是什么,以及如何使其正常运行?

我的代码如下:

<?php
require "require/class.config.php";
if(isset($_GET["id"])) {
    $id = intval($_GET["id"]);
    if($id <=0) {
        die("This file ID is invalid.");
    } else {
        $query = "SELECT `name`, `mime`, `size`, `data`, `url`, `owner`, `extension`, `created` FROM `files` WHERE `id` = {$id}";
        $result = $db->query($query);
        if($result) {
            if($result->rowCount() === 1) {
                $row = $result->fetch(PDO::FETCH_ASSOC);
                if($result["extension"] == ("mp4")) {
                    // DISPLAY VIDEO PLAYER HERE
                } else {
                    header("Content-Type: " . $row["mime"]);
                    header("Content-Length: " . $row["size"]);
                    while(@ob_end_clean());
                    header("Content-Disposition: attachment; filename=" . $row["name"]);
                    echo $row["data"];
                }
            } else {
                echo "The file you were looking for was not found.";
            }

            @mysqli_free_result($result);
        } else {
            echo "Database query unsuccessful.";
        }
        @mysqli_close($db);
    }
} else {
    echo "No file ID was received.";
}

DB:

<?php
ob_start();
session_name("session");
session_start();
date_default_timezone_set("Europe/London");
$dbusername = "root";
$dbpassword = "";
$host = "localhost";
$dbname = "safe";
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
);
try {
    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $dbusername, $dbpassword, $options);
}
catch(PDOException $ex) {
    die("Database connection unsuccessful.");
}

0 个答案:

没有答案