2013-11-14 31 views
0

我正尝试创建一个跨平台的应用程序,并且我想使用Codeigiter将不同设备中的图像保存在数据库中。我可以从设备中选择图像,但是我很困惑并且不知道如何保存图像并从数据库中检索图像。将图像保存到数据库中无法正常工作

控制器

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'); 
} 

型号

function addstatesoothing($data) { 
    $this->load->database(); 
    $this->db->insert('soothing', $data); 
    return $data; 
} 

查看

<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> 
+0

混淆和库存并不可以在这里解决了一个问题,所以你有什么编程问题? –

+0

上面的代码不会保存图像文件在数据库中 – Omade

+0

啊,我以为'$ this-> db-> insert('soothing',$ data);'这样做了,但有人已经做好了你的功课;-) –

回答

1

要保存图像数据库:

1 - 数据库您的图像场应当是BLOB类型。

2 - 从图像中获取文件内容:

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

3 - 在你的BLOB类型字段将它保存在数据库

mysql_query("insert into images (image) values ('$image') "); 

我不知道,如果你使用其他方法来保存数据库中的数据,你的代码有点不一样,但是就是这样,对吧?最后它会插入数据库中的一行,如果您有任何疑问,请在这里留言,但在这一点上很容易,火腿?现在让我们去retriving图像

首先,在数据库存储图像内容是一个真正的错误,但这不是问题吧?所以,我不是这种方法的专家,但我会怎样:

创建像image.php的另一个页面,通过参数

image.php接收一些ID或图像名称:

<?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']; 
} 
?> 

很高兴,你有反应的图像就像任何* .jpg文件一个漂亮的*。PHP文件,所以现在把它在你的HTML

SomePage.html

<img src="image.php?id=12345" /> 

好,全部完成,

为什么我告诉他们储蓄图像内容是坏?: SO家伙是真棒,我们有惊人的材料来解释为什么在数据库中保存的图像是坏的!你可以检查一下吧,这里有:

  1. Storing Images in DB - Yea or Nay?
  2. Store images in database or on file system