要存储到数据库中的音频文件

时间:2015-07-08 04:25:51

标签: php mysql media

在此链接之前:What is the best way to store media files on a database?

答案说明:

( 我所知道的每个存储大量大文件的系统都将它们存储在数据库的外部。您将文件的所有可查询数据(标题,艺术家,长度等)以及文件的部分路径存储在数据库中。当需要检索文件时,您将提取文件的路径,在其前面添加一些文件根目录(或URL),然后返回该文件。 )

我的问题是:

a)如何存储文件的部分路径?

b)如何提取文件的路径?

c)你如何预先添加一些文件root并将其返回?

(对不起,我很新,这一点我不太明白。任何输入或例子都很可爱。)

是的,这些是我上传文件的代码,我只是不知道检索位。

<?php
if(isset($_FILES['uploaded_file'])) {   

if($_FILES['uploaded_file']['error'] == 0) {
    // Connect to the database
    $dbLink = new mysqli('localhost', 'root', '', 'spellingbee');
    if(mysqli_connect_errno()) {
        die("MySQL connection failed: ". mysqli_connect_error());
    }

    // Gather all required data
    $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
    $location = $dbLink->real_escape_string($_FILES['uploaded_file']['location']);
    $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
    $size = intval($_FILES['uploaded_file']['size']);

    // Create the SQL query
    $query = "
        INSERT INTO `file` (
            `name`, `location`, `size`, `data`, `created`
        )
        VALUES (
            '{$name}', '{$location}', {$size}, '{$data}', NOW()
        )";

    // Execute the query
    $result = $dbLink->query($query);

    // Check if it was successfull
    if($result) {
        echo 'Success! Your file was successfully added!';
    }
    else {
        echo 'Error! Failed to insert the file'
           . "<pre>{$dbLink->error}</pre>";
    }
}
else {
    echo 'An error accured while the file was being uploaded. '
       . 'Error code: '. intval($_FILES['uploaded_file']['error']);
}

// Close the mysql connection
$dbLink->close();
 }
 else {
echo 'Error! A file was not sent!';
 }

 // Echo a link back to the main page
  echo '<p>Click <a href="form.html">here</a> to go back</p>';
 ?>

1 个答案:

答案 0 :(得分:0)

据我了解您的问题,您想上传音频文件并将其名称保存到数据库,然后您想要检索它。

要在所有验证之后这样做(我正在编写此代码,就好像要创建目录一样)

if(is_dir("audio")
  {}
else
{mkdir("audio");}

$path = "http://www.domain.com/audio/".$_FILES['file']['name']; //prepend any path

// insert in database
move_uploaded_file($_FILES['file']['tmp_name'],$path);

并且要重温它:

只需从数据库中获取路径的值。

从数据库中获取:

  $q = mysql_query("select * from tablename");

  while($r = mysql_fetch_array($q))
  {
     $path = $r['columnNameofPathinDatabase'];
     echo $path;
  }