无法将音频文件作为base64上传到数据库中

时间:2017-12-07 17:17:56

标签: php html mysql database server

我正在尝试将音频文件上传到我的服务器并将其详细信息上传到我的数据库中,但我想将其以base64格式上传到我的数据库中。

下面是track.php(用于上传曲目的PHP)

<?php
include("config.php");

if(isset($_POST['but_upload'])){

    $track = $_POST['name'];
    $link = $_POST['link'];
    $name = $_FILES['file']['name'];
    $target_dir = "uploads/tracks/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);

    // Select file type
    $audioFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

    // Valid file extensions
    $extensions_arr = array("mp3","mp4","ogg");

    // Check extension
    if( in_array($audioFileType,$extensions_arr) ){

        // Convert to base64 
        $audio_base64 = base64_encode(file_get_contents($_FILES['file']['tmp_name']) );
        $audio = 'data:audio/'.$audioFileType.';base64,'.$audio_base64;

        // Insert record
        $query = "insert into tracks(name,location,correlation) values('".$track."','".$audio."','".$link."')";

        mysqli_query($con,$query) or die(mysqli_error($con));

        // Upload file
        move_uploaded_file($_FILES['file']['tmp_name'],'uploads/tracks/'.$name);

    }

}

header('Location: index.html');
?>

以下是config.php

<?php

$host = "localhost";    /* Host name */
$user = "root";         /* User */
$password = "";         /* Password */
$dbname = "samuel_newday";   /* Database name */

// Create connection
$con = mysqli_connect($host, $user, $password,$dbname);

// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());

}
下面的

是上传音频文件的html文件的一部分

<div class="bgrid">
                <h3>Tracks.</h3>
                <p style="color: white">Step 2.<br /><span style="font-size: smaller">Upload Tracks Individually.</span></p>    
                <p>
                 <div class="image_upload_div">
                      <form action="tracks.php" enctype="multipart/form-data">
                          <input type="text" name="name" placeholder="Track Name" />
                          <select name="link" style="width: 100%;">
                              <option>--- Select Album Art ---</option>
                              <option></option>
                          </select>
                          <input type="file" name='file' style="width: 100%; text-align: left; margin-top: 8%; margin-bottom: 8%" />
                          <input type='submit' value='Upload' name='but_upload' style="width: 100%; text-align: center">
                      </form>
                  </div>
                </p>    
          </div>

有人可以帮我解决这个问题,我想知道为什么音频文件没有上传以及我该怎么办。

0 个答案:

没有答案