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();  
?> 
2
  • 1
    possible duplicate of How can I store and retrieve images from a MySQL database using PHP? Commented Jun 13, 2011 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. Commented Jun 13, 2011 at 14:47

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I think this is the way I will be headed now. Thanks!
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')");

Comments

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();  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.