0

I have the same problem as this one Insert Blobs in MySql databases with php but my files are music files and somehow the answers are not working for me.

This is my database:

+---------+-----------+------+----+--------+--------------+   
| Field   |    Type   | Null |Key |Default |Extra         |  
+---------+-----------+------+----+--------+--------------+  
|music_id |int(11)    |  NO  |PRI |NULL    |auto_increment|  
|mp3      |longblob   |  YES |    |NULL    |              |  
|mime     |varchar(30)|  NO  |    |NULL    |              |
+---------+-----------+------+----+--------+--------------+  

This is my form in html:

<form method="post" action="uploadFile.php" enctype='multipart/form-data'>
    <label>Upload a file with blob datatype</label><br>
    <input type="file" name="Upfile" required="" accept= "audio/*">
    <input type="submit" name="submit_Upfile" value="Submit">
</form>

This is my php file:

<?php 
$host = "localhost";
$user = "root";
$pword = "";
$db = "musics";
$con = mysqli_connect($host,$user,$pword,$db);

if($con)
{
if(isset($_POST["submit_Upfile"]))
{
    $audio_name= mysqli_escape_string($con,basename( $_FILES["Upfile"]["name"] ));

    $audio_content = mysqli_escape_string($con,file_get_contents($_FILES['Upfile']['tmp_name']));

    $audio_size = getimagesize($_FILES["Upfile"]["tmp_name"]);
    $audio_content = base64_encode($audio_content);
    echo "audio_size here => ".$audio_size["mime"]."<br>";
    echo "audio_name here => ".$audio_name."<br>";
    echo "audio_content here => ".$audio_content."<br>";

    $insert_audio_to_db = mysqli_query($con,"INSERT INTO `mp3s`( `mp3` ,`mime`) VALUES ('".$audio_content."','".$audio_size['mime']."')" );
    if($insert_audio_to_db)
    {
        echo "Insert successful.";
    }else
    echo "Insert failed.";
}
}
else
echo "Connection error.";

?>

I tried to echo some things and this is what I got:

audio_size here => 
audio_name here => iMessage Send and Receive Sounds.mp3
audio_content here => [too long to display]
Insert failed.

Did I miss something?

Rence
  • 124
  • 7

1 Answers1

0

Since I have found the answer of this question with the help of my colleague Diana Llona. We will share it to others who are experiencing the same problem.

Problem: The main problem here is that xampp has a default max size of upload files.

To change it follow these steps:

  1. Open your Xampp Control Panel. See image here.
  2. On the row of your Apacheclick the config button. Find the PHP (php.ini) file.
  3. It will open the php.ini file for you and there, find the upload_max_filesize and post_max_size.
  4. Change it to your desired size then save and close the file. (I just changed the post_max_size this and upload_max_filesize that big because I just want to.)

Next is to change the innodb_log_file_size of the MySQL.

  1. Click the config button for your MySQL on the Xampp Control Panel. See image here
  2. Then find the innodb_log_file_size and change it to your desired size then save and close the file. See image here

After you've done these steps, restart your Apache and MySQL by clicking the Stop button and Start it again.

Community
  • 1
  • 1
Rence
  • 124
  • 7