1

I am trying to store image on my mysql database. I know that it may not be the smartest thing to do, but this job requires it. I have looked online on how to store an image using php code, however every time I try to follow the guidelines it seems like a random problem pops up. With this code am using right now, I've managed to get all the information stored in the database except the image and the image name. Any suggestions on how to make it work?

 $connect = mysqli_connect("host", "user", "pass", "dbname");  
 if(!empty($_POST))  
 {  
      $output = '';  
      $title = mysqli_real_escape_string($connect, $_POST["title"]);  
      $image = addslashes($_FILES['image']['tmp_name']);
      $name = addslashes($_FILES['image']['name']);
      $image = file_get_contents($image);
      $image = base64_encode($image);
      $text2 = mysqli_real_escape_string($connect, $_POST["text2"]);  
      $text1 = mysqli_real_escape_string($connect, $_POST["text1"]);       
      $dtime = date('Y-m-d H:i:s'); 
      $query = "  
      INSERT INTO news(title, image, name, text2, text1, dtime)  
      VALUES('$title', '{$image}', '$name', '$text2', '$text1', '$dtime');  
      ";  

I have tried different options, adding $connect into $image and $name to make it like the rest or changing addslashes into mysql_real_escape_string.

I need some help to get it to work,if you have the time I'll appreciate it!

1 Answers1

0

It's probably because you are adding slashes to the file path before reading the file.

<?php
$connect = mysqli_connect("host", "user", "pass", "dbname");  
 if(!empty($_POST))  
 {  
      $output = '';  
      $title = mysqli_real_escape_string($connect, $_POST["title"]);

      // Replace below two lines...
      //   $image = addslashes($_FILES['image']['tmp_name']);
      //   $image = file_get_contents($image);
      // With:
      $image = file_get_contents($_FILES['image']['tmp_name']);
      $name = addslashes($_FILES['image']['name']);
      $image = base64_encode($image);
      $text2 = mysqli_real_escape_string($connect, $_POST["text2"]);  
      $text1 = mysqli_real_escape_string($connect, $_POST["text1"]);       
      $dtime = date('Y-m-d H:i:s'); 
      $query = "  
      INSERT INTO news(title, image, name, text2, text1, dtime)  
      VALUES('$title', '{$image}', '$name', '$text2', '$text1', '$dtime');  
      ";  

Also you probably don't want to store a base64_encoded file in your database as it increases storage size (Base64: What is the worst possible increase in space usage?). Store the image as a blob (= binary data) in your database.

Considering this didn't work for you, try the following to debug:

<?php
// Print out the submitted files and the data.
var_dump($_FILES);
// Check to see if it's not empty, whether the submitted filename is actually "image", 


echo (file_exists($_FILES['image']['tmp_name']) ? 'File exists!' : 'File does not exist'), PHP_EOL;

echo (is_readable($_FILES['image']['tmp_name']) ? 'Can read the file!' : 'Can not read the file'), PHP_EOL;

$file = file_get_contents($_FILES['image']['tmp_name']);

echo 'File size: ', strlen($file), ' bytes (base64: ', strlen(base64_encode($file)), ' bytes)';

Try that and see if anything is out of the ordinary...

Community
  • 1
  • 1
Kalkran
  • 324
  • 1
  • 8
  • @MattiasMatte see my edit, maybe it can help you figure out the source of the problem – Kalkran Nov 25 '16 at 13:51
  • Thank you for the help. I managed to solve it. I think the problem was maybe me using ajax,when Iremoved it the files got uploaded! ;D – Mattias Matte Nov 25 '16 at 14:15