0

I want to upload a .wav files as blob to database. I do not want to upload it to a directory.

I think I have successfully uploaded the file as blob to database, but when I want to download the file, it downloads a plain file without extension name. I was expecting something like .wav as the file extension etc.

Here is my table

create table content(
    id int primary key auto_increment,
    cid int(11),
    data_file varchar(200),
    data longblob);

alter table content add column(
    name1 varchar(200),
    type1 varchar(200),
    size1 varchar(200));

Here is the code for upload

<?php
if(isset($_FILES['file']) and !$_FILES['file']['error']){

echo $tt= strip_tags($_POST['idb']);
    //$fname = "155" . ".wav";

$fname = time() . ".wav";

$tmpName  = $_FILES['file']['tmp_name'];
$name  = $_FILES['file']['name'];

//move_uploaded_file($_FILES['file']['tmp_name'], "uploads/" . $fname);

include('db.php');


  $type1= $_FILES["file"]["type"];
   $size1=$_FILES["file"]["size"];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);


$statement = $db->prepare("INSERT INTO content (cid,data_file,data,name1,type1,size1) values
(:cid,:data_file,:data,:name1,:type1,:size1)");

$query= $statement->execute(array( 

':cid' => $tt,
':data_file' => $fname,
':data' => $content,
':name1' => $name,
':type1' => $type1,
':size1' => $size1
));

  fclose($fp); 


// move_uploaded_file($_FILES['file']['tmp_name'], "./uploads/" . $fname);
}

?>

Here is the code for download

<?php

include('db.php');

$result = $db->prepare('SELECT * FROM content');

        $result->execute(array());

$count = $result->rowCount();

while($row = $result->fetch()){


$content=htmlentities($row['data']);
$name=htmlentities($row['name1']);
$type=htmlentities($row['type1']);
$size=htmlentities($row['size1']);
$bb=htmlentities($row['bb']);
}

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;

?>

Can someone help me fix the issue of not been able to download the .wav files from database?

Pang
  • 9,564
  • 146
  • 81
  • 122
  • whats the name in the db show? –  Mar 01 '18 at 02:27
  • 1
    Why do you want to upload audio to your DB? I've been down this rabbit hole myself and always ended up taking another route. See this question/answer for more info: https://stackoverflow.com/questions/154707/what-is-the-best-way-to-store-media-files-on-a-database S3 on AWS is a good solution if you don't want to store them on your local machine – TCooper Mar 01 '18 at 02:38
  • Agreed with @TCooper. "I must save giant files directly to the DB" has been a sign of a misconception/misunderstanding 100% of the times I've run into it. – ceejayoz Mar 01 '18 at 02:57
  • `addslashes` on your data going into the db will corrupt it, since the prepared statement will also handle that. `htmlentities` on your output data is going to corrupt the binary. Hopefully you have set the `max_allowed_packet` larger than default. .... what the others said. This is a rabbit hole you don't want to go down. – IncredibleHat Mar 01 '18 at 03:19

0 Answers0