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?