0

Okay so I am running into issues inserting a file into my database as a blob. I am able to upload the file to a folder on my database but I now want to insert the file into my database as a blob. I know this isnt generally ok to do but I WANT TO DO IT. Anyways I was wondering if anyone had any PHP code to insert a file into a blob table? I've tried using this to no avail maybe I am just not writing the SQL statement right?

<?php 
 $target = "./"; 
 $target = $target . basename( $_FILES['uploadedfile']['name']); 
 $file=($_FILES['uploadedfile']['name']); 
 if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
 }else{
 echo "There was an error uploading the file, please try again!";
 }
 mysql_connect("localhost","root","");
 mysql_select_db("ivrsupport");
 $sql=mysql_query("insert into CUSTOMER (AUDIO_FILE)values
   ('$file') where PHONE_NUMBER = ('15555215554')");
 $r=mysql_query($sql);
 if(!$r){
 echo "Error in query: ".mysql_error();
 }  
mysql_close();  
?> 
IZI_Shadow_IZI
  • 1,921
  • 4
  • 30
  • 59
  • 1
    possible duplicate of [How can I store and retrieve images from a MySQL database using PHP?](http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database-using-php) – Mat Jun 13 '11 at 14:45
  • From what I can see in your question, you're not actually reading the file data, just downloading it to your server, and trying to insert a filename in that database column. The linked post above shows you how to read a file from disk and insert it into a blob. The "Related" links on the right will inform you of some restrictions to this. – Mat Jun 13 '11 at 14:47

3 Answers3

2

I would avoid storing files in the database. That's what FileSystems are for and are much better at doing so. There are millions of posts out there in the blogosphere explaining why not to. Even for a small application I wouldn't do it. It just feels dirty. A blob in the database holds no metadata about the file itself where as a FS can be queried in many ways to gleem information like file size, modification times, permissions etc... My $0.02

eroomydna
  • 1,261
  • 9
  • 4
1

You need to read the file into a variable and then insert it.. giving it the filename only wont do.

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

$sql=mysql_query("insert into CUSTOMER (AUDIO_FILE)values
   ('$content') where PHONE_NUMBER = ('15555215554')");
Jay Sidri
  • 6,271
  • 3
  • 43
  • 62
0

$file simply contains the name of the file not it's contents.

Try the following:

<?php 
 $target = "./"; 
 $target = $target . basename( $_FILES['uploadedfile']['name']); 
 $file=($_FILES['uploadedfile']['name']); 
 $fileContents = file_get_contents($file);
 if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
 echo "The file ".  basename( $_FILES['uploadedfile']['name']).
 " has been uploaded";
 }else{
 echo "There was an error uploading the file, please try again!";
 }
 mysql_connect("localhost","root","");
 mysql_select_db("ivrsupport");
 $sql=mysql_query("insert into CUSTOMER (AUDIO_FILE)values
   ('$fileContents') where PHONE_NUMBER = ('15555215554')");
 $r=mysql_query($sql);
 if(!$r){
 echo "Error in query: ".mysql_error();
 }  
mysql_close();  
daemonofchaos
  • 795
  • 4
  • 13