How to upload picture in BLOB instead of path file under the same ID?


I'm trying to upload pictures into the database in a BLOB. Whenever I use my current script it puts the file path in the BLOB instead of the jpg file. I've tested it by changing blob to text.

This is what I currently get whenever I try to upload a picture:


I'm currently using ['tmp_name'] and I think the is the cause of this problem. I was wondering what I have to change.

Here is my code:

$tipe_file1 = $FILES['image1']['tmpname'];
$tipe_file2 = $FILES['image2']['tmpname'];
$tipe_file3 = $FILES['image3']['tmpname'];
$tipe_file4 = $FILES['image4']['tmpname'];
$prijs = $_POST['prijs'];
$naam = $_POST['fname'];
$beschrijving = $_POST['desc'];
$maat1 = $_POST['maat1'];
$maat2 = $_POST['maat2'];
$maat3 = $_POST['maat3'];
$maat4 = $_POST['maat4'];

$aMyUploads = array();
foreach ($_FILES as $aFile) {
    if(0 === $aFile['error']){
        $newLocation = ''.$aFile['tmp_name'];
        if(0 === $aFile['error'] && (false !== move_uploaded_file($aFile['tmp_name'], $newLocation))){
            $aMyUploads[] = $newLocation;

        } else {
            $aMyUploads[] = '';
$stmt = $conn->prepare("INSERT INTO `producten` (naam, beschrijving, prijs, maat1, maat2, maat3, maat4, image1, image2, image3, image4) VALUES
(:naam, :beschrijving, :prijs, :maat1, :maat2, :maat3, :maat4, :image1, :image2, :image3, :image4)");
$stmt->execute(array(":naam"=>$naam, ":beschrijving"=>$beschrijving, ":prijs"=>$prijs, ":maat1"=>$maat1, ":maat2"=>$maat2, ":maat3"=>$maat3, ":maat4"=>$maat4, ":image1"=>$aMyUploads[0], ":image2"=>$aMyUploads[1], ":image3"=>$aMyUploads[2], ":image4"=>$aMyUploads[3]));


At the moment, you're using move_uploaded_file() to move the image file from it's temporary upload location to the final location. This tells you that, once it's been uploaded, the file is in $aFile['tmp_name']. So it's just a case of using something like file_get_contents($aFile['tmp_name']) to load the contents of the image file (rather than the name of the file) into a string, and then stick that into your insert query instead of the name.

Are you sure you really want to store the image directly inside the database? It has advantages and disadvantages, I would imagine.


Hello thank you:

I changed the code to this

$newLocation = ''.file_get_contents($aFile['tmp_name'])['tmp_name'];
                if(0 === $aFile['error'] && (false !== file_get_contents($aFile['tmp_name']))){

Now it gives me this error:

Warning: Illegal string offset 'tmp_name' in C:\xamppv2\htdocs\fabriek\invullen.php on line 70

And yes I want to store it in a database. There are only going to be around 30 pictures in there so not to many.


$newLocation = ''.file_get_contents($aFile['tmp_name'])['tmp_name'];

What's the reason for pre-pending the '' here? And why is ['tmp_name'] in twice? Which line is line 70?

Also in your second line there's no need to call file_get_contents() again - just compare it to $newLocation which will already contain the file or not.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.