dqwh1208 2015-07-08 04:25
浏览 122

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

Prior to this link:What is the best way to store media files on a database?

The answer stated:

( Every system I know of that stores large numbers of big files stores them externally to the database. You store all of the queryable data for the file (title, artist, length, etc) in the database, along with a partial path to the file. When it's time to retrieve the file, you extract the file's path, prepend some file root (or URL) to it, and return that. )

My questions are:

a)How do you store the partial path of the file?

b)How do you extract the file's path?

c)How do you prepend some file root and return it?

(Sorry I am very new and this bit I don't quite get. Any input or examples would be lovely.)

Btw, these are my codes for uploading the file, I just don't know the retrieve bit.

<?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条回答 默认 最新

  • doukanxi4246 2015-07-08 04:56
    关注

    As far as I understand your problem you want to upload a audio file and save its name to database and then You want to retrieve it.

    To do so just after your all validations (I am writing this code as if want to create a directory)

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

    And to retrive it:

    just fetch the value of path from database.

    Fetch From DB:

      $q = mysql_query("select * from tablename");
    
      while($r = mysql_fetch_array($q))
      {
         $path = $r['columnNameofPathinDatabase'];
         echo $path;
      }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 mySQL5.7.34安装遇到的问题
  • ¥15 结构功能耦合指标计算
  • ¥20 visual studio中c语言用ODBC链接SQL SERVER
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊