Reputation: 47
I am trying to create a cross platform application and I would like to save images from different devices in the database using Codeigiter. I am able to select the images form the device but I am confused and stocked on how to save images and retrieving them from the database.
Controller
function addstatesoothing() {
// write user data into the user database
$config['upload_path'] = './';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
$image_path = $this->upload->data('soothing-img');
$data = array(
'soothing-img' => $image_path[full_path],
'soothing-title' => $this->input->post('soothing-title'),
);
$this->favourite_db->addstatesoothing($data);
$this->load->view('soothing');
}
Model
function addstatesoothing($data) {
$this->load->database();
$this->db->insert('soothing', $data);
return $data;
}
View
<form method="post" action="addstatesoothing">
<!-- <button data-theme="d" onclick="capturePhoto();">Capture Photo</button> -->
<a data-theme="d" onclick="getPhoto(pictureSource.PHOTOLIBRARY);">Click to Add Pictures</a>
<img style="width:100%;" id="largeImage" src="" name="soothing-img"/> <br>
<input type="text" name="soothing-title" id="Climbed a Mountain" value="" placeholder="Climbed a Mountain"/>
<button data-theme="a">Save Memory</button>
</form>
Upvotes: 0
Views: 183
Reputation:
The first thing in this question is your form is not able to upload image because your not using multipart form data
you can try like this
<form enctype="multipart/form-data"></form>
Upvotes: 0
Reputation: 4446
To save image in database:
1 - Your image field on database should be BLOB type.
2 - Get file content from your image:
$image = file_get_contents($_FILES['image']['tmp_name']);
3 - Save it in your BLOB type field on database
mysql_query("insert into images (image) values ('$image') ");
I don't know if you use another approach to save data in database, your code is a bit different , but that's the way, right? In the end it'll be a row inserted in database, If you have any doubt, comment here, but at this point is all easy, ham? now let's go to retriving images
First of all, storing images content in database is a real mistake, but that isn't the question right? So I'm not an expert in this approach, but is how I would:
Create an another page like image.php, receiving some id or image name by parameter
image.php:
<?php
$id = $_GET['id'];
$query = "SELECT image FROM images WHERE `id`=$id";
$result = mysql_query($query);
header("Content-type: image/jpeg"); //if is another format like .png change here
while($row = mysql_fetch_assoc($result))
{
echo $row['image'];
}
?>
Nice, you have a nice *.php file that response an image just like any *.jpg file, so now call it in your HTML
SomePage.html:
<img src="image.php?id=12345" />
Ok, all done,
but why I told that saving image content is bad?: SO fellas are awesome, we have amazing material to explain why saving images in database is BAD! You could check it, here's:
Upvotes: 1