0

我目前正在开发一个允许您上传到屏幕的画廊,我想知道我应该如何将图像保存到数据库中,以便每个选择的图像都特定于每个用户。我已经完成了个人资料图片上传,它将每个用户的文件链接保存在数据库中,现在由于我试图为每个用户保存一组图像,我不确定你将如何存储一组图像一个身份证。怎么可能做到这一点?目前我的画廊数据库有 2 个字段,用户和画廊图像,但我认为这应该成为画廊图像,因为它包含图像名称的选择。在将文件名位置存储为 varchar 之前,我的个人资料图像。请指出我应该如何在一个字段中保存一组图像,或者如果它不可能。

这是我到目前为止所拥有的:

控制器:

class Gallery extends CI_Controller {



  function __construct()
   {
    // Call the parent construct
    parent::__construct();

    $this->load->model("profiles");
    $this->load->model("gal_model");
    $this->load->helper(array('form', 'url'));

   }


  function index()
  {

            //The commented out lines below signify what I have so far and when I uncomment them the image will no longer upload
            // At the moment the images just upload to the page
    //$this->load->library('upload');
    $username = $this->session->userdata('username');




    //$file_data = $this->upload->data();

    //$image = 'gallery_path'.$file_data['file_name'];

    //$data['image'] = 'gallery_path'.$file_data['file_name'];

    if($this->input->post('upload'))
    {

        $this->gal_model->do_upload();

    }




    //$this->gal_model->putGalleryImage($username, $image);


    $viewData['username'] = $username;

    $data['images'] = $this->gal_model->get_images();

    $this->load->view('shared/header');
    $this->load->view('gallery/galtitle', $viewData);
    $this->load->view('shared/nav');
    $this->load->view('gallery/galview', $data);
    $this->load->view('shared/footer');

}

}

模型:

class Gal_model extends CI_Model
{
var $gallery_path;
var $gallery_path_url;

function Gal_model()
{
    parent::__construct();

    $this->gallery_path = 'web-project-jb/assets/gallery';
    $this->gallery_path_url = base_url().'web-project-jb/assets/gallery/';
}   


function exists($username)
{
    $this->db->select('*')->from("gallery")->where('user', $username);
    $query = $this->db->get();

    if ($query->num_rows() > 0)
    {

        return true;
        /*
         echo "user $user exists!";
        $row = $query->row();
        echo " and his profileimage is $row->profileimage";
        */
    }

    else

    {

        return false;
        //echo "no such user as $user!";
    }

}


    //this function is aiming to put the galleryimages into the database
function putGalleryImage($username, $image)
{


    $record = array('user' => $username, 'galleryimage' => $image);
    //$this->session->set_userdata($image);
    if ($this->exists($username))
    {
        $this->db->where('user', $username)->update('gallery', $record);


    }
    else
    {
        $this->db->where('user', $username)->insert('gallery', $record);

    }

}

function do_upload ()
{
   $config = array(
  'allowed_types' => 'gif|jpg|jpeg|png',
  'upload_path' => $this->gallery_path,
  'max_size'   => 10000     
            );


    $this->load->library("upload", $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    $config = array(
        'source_image'  => $image_data["full_path"],
        'new_image'     => $this->gallery_path. '/thumbs',
        'maintain_ration'   => true,
        'width' => 150,
        'height' => 100     


            );

    $this->load->library("image_lib", $config);
   $this->image_lib->resize();
}


function get_images()
{
    $files = scandir($this->gallery_path);
    $files = array_diff($files, array('.', '..', 'thumbs'));

    $images = array();

    foreach ($files as $file){
        $images[] = array(
            'url'   => $this->gallery_path_url.$file,
            'thumb_url' => $this->gallery_path_url.'thumbs/'.$file


                );

    }

    return $images;
}


 }

我的画廊视图:

  <?php if ( is_array($images) && (count($images)>0) ):
   foreach($images as $image): ?>
   <div class="thumb">
       <a href="<?php echo $image['url']; ?>">
          <img src ="<?php echo $image['thumb_url']; ?>"/>
       </a> 
       <br>
   </div>
<?php endforeach; else:  ?>
    <div id = "blank_gallery">Please upload an Image</div>
<?php endif; ?>
4

2 回答 2

0

您可以简单地使用唯一的名称(例如当前的 unix 时间戳)命名每个上传,并将用户 ID 添加到名称的前面。现在您可以调用任何图片,因为您知道文件名的一部分是用户 ID。您不需要将链接存储在数据库中。

于 2012-12-12T02:49:06.963 回答
0

您可以将图像路径数组转换为逗号分隔的字符串。然后,您可以轻松地将其保存到数据库中。

于 2012-12-12T02:28:33.217 回答