how to store & get file from database using codelgniter php & my sql

Viewed 16

i can store the file into database but can't get / display the file stored in database

the file is stored in the database but can't display from the database

i am using following code

<?php

class file_upload extends CI_Controller
{

    public function index()
    {
        echo "  <head>
        <title>Upload Image To Database</title>
      </head>
      <body>
        <!-- (A) HTML FILE UPLOAD FORM -->
        <form method='post' enctype='multipart/form-data'>
          <input type='file' name='upload' accept='.png,.gif,.jpg,.webp' required/>
          <input type='submit' name='submit' value='Upload Image'/>
        </form>";

        if (isset($_FILES['upload'])) 
        {
          $imgname=$_FILES["upload"]["name"];
          $data["img_name"]=$_FILES['upload']['name'];
          $data["img_data"]=file_get_contents($_FILES["upload"]["tmp_name"]);
          $this->db->insert('images',$data);
          $data=$this->db->query('select * from images where img_name="$imgname"');
          foreach ($data->result() as $row)
          {
            print_r($row->img_data);
            echo "<img src='data:image/jpg;charset=utf8;base64, ";
            echo "base64_encode($row->img_data)";
            echo "height=100 width=100>";
          }

        }
    }  
}

?>
1 Answers

You were close. You're base64_encode cannot be executed in a string interpolation like that. You can't run functions inside a string interpolation at all. This should work.

<?php

class file_upload extends CI_Controller
{

    public function index()
    {
        echo "  <head>
        <title>Upload Image To Database</title>
      </head>
      <body>
        <!-- (A) HTML FILE UPLOAD FORM -->
        <form method='post' enctype='multipart/form-data'>
          <input type='file' name='upload' accept='.png,.gif,.jpg,.webp' required/>
          <input type='submit' name='submit' value='Upload Image'/>
        </form>";

        if (isset($_FILES['upload'])) 
        {
          $imgname=$_FILES["upload"]["name"];
          $data["img_name"]=$_FILES['upload']['name'];
          $data["img_data"]=file_get_contents($_FILES["upload"]["tmp_name"]);
          $this->db->insert('images',$data);
          $data=$this->db->query('select * from images where img_name="$imgname"');
          foreach ($data->result() as $row)
          {
            print_r($row->img_data);
            echo "<img src='data:image/jpg;charset=utf8;base64, ";
            echo base64_encode($row->img_data);
            echo "'height=100 width=100>";
          }

        }
    }  
}

?>

Also, since you've hard coded jpg, if you've uploaded anything other than that it may not show in the image tag correctly.

Overall it would be better to store the image in a directory somewhere and then store, in your database, information about the image. type, width, height, location, etc.

Also, since you're using CI I recommend checkout out their handy functions for handling files. Specifically https://www.codeigniter.com/user_guide/incoming/incomingrequest.html?highlight=file%20upload#uploaded-files and https://www.codeigniter.com/user_guide/helpers/filesystem_helper.html

Happy coding!

Related