将视频存储在数据库中然后检索它

时间:2014-03-25 08:25:14

标签: php

有人可以告诉我这段代码我做错了什么,我将localhost连接到名为video的数据库。然后我插入表位置及其列位置值$videoLocation

我得到的结果是当我打开浏览器时

  <?php
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
  ?>

我想通过此代码尝试实现的目的是,一旦用户提交了该视频,该视频就会显示在我的网页上。

    <?php        
    $allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
    $allowType =   array("video/mp4","audio/mp3","audio/wma","image/png","image/gif","image/jpeg");
    $maxSize = 20000000000000;
    $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    $pathToUpload = 'upload/';

      if( in_array($_FILES["file"]["type"], $allowType) && in_array($extension,   $allowedExts) && $_FILES["file"]["size"] <= $maxSize)
         {
      if ($_FILES["file"]["error"] > 0)
         {
      echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
          else
           {
           echo "Upload: " . $_FILES["file"]["name"] . "<br />";
     echo "Type: " . $_FILES["file"]["type"] . "<br />";
     echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

     if (file_exists($pathToUpload . $_FILES["file"]["name"]))
     {
         echo $_FILES["file"]["name"] . " already exists. ";
     }
     else
     {
         move_uploaded_file($_FILES["file"]["tmp_name"], $pathToUpload . $_FILES["file"] ["name"]);
          $videoLocation = "upload/".$_FILES['file']['name'];
        // now insert $videoLocation into a database table
        $db = new mysqli('127.0.0.1', 'root', '', 'video');
        INSERT INTO location (location)
        VALUES ($videoLocation)
       //so you can fetch it on whatever page you feel like
             }
       }
       }
           else
          {
          echo "Invalid file";
           }

     ?>  

2 个答案:

答案 0 :(得分:0)

试试这个

$db = new mysqli('127.0.0.1', 'root', '', 'video');
$stmt = $db->prepare("INSERT INTO location (location) VALUES (?)");
$stmt->bind_param('s', $videoLocation);
$stmt->execute();
//so you can fetch it on whatever page you feel like

答案 1 :(得分:0)

我最近为想要将图像文件存储在数据库中的其他人回答了一个问题。以下是如何做到这一点:

1)数据库列必须定义为longblob。

接下来,代码使用PDO而不是mysqli。

// database connection...
$dsn = 'mysql:host=localhost;dbname=testmysql';
$username = 'test';
$password = 'test';
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$connection = new PDO($dsn, $username, $password, $options);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

然后你需要一个打开的文件句柄,如下所示:

$fileVideo  = fopen($videoLocation,'rb');

然后准备语句并绑定文件:

$stmt = $connection->prepare("INSERT INTO location (location) VALUES (?)");
$stmt->bindParam(1, $fileVideo, PDO::PARAM_LOB);

$connection->beginTransaction();
$stmt->execute();
$connection->commit();
fclose($fileVideo);

我还没有对您的文件进行上述测试,但原始代码与上面的内容相似,但名称更改工作正常。