Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将配置文件的库图像保存到数据库中?_Php_Codeigniter_Gallery - Fatal编程技术网

Php 如何将配置文件的库图像保存到数据库中?

Php 如何将配置文件的库图像保存到数据库中?,php,codeigniter,gallery,Php,Codeigniter,Gallery,我目前正在开发一个允许你上传到屏幕上的图库,我想知道如何将图像保存到数据库中,以便每个用户都可以选择特定的图像。我已经完成了一个配置文件图像上传,它将每个用户的文件链接保存到数据库中,现在由于我试图为每个用户保存一组图像,我不确定如何为一个ID存储一组图像。这是如何做到的?目前我的画廊数据库有两个字段,用户和galleryimage,但我认为这应该成为galleryimage,因为这是一个选择的图像名称。我的配置文件图像之前只是将文件名位置存储为varchar。请向我指出如何在一个字段中保存图像

我目前正在开发一个允许你上传到屏幕上的图库,我想知道如何将图像保存到数据库中,以便每个用户都可以选择特定的图像。我已经完成了一个配置文件图像上传,它将每个用户的文件链接保存到数据库中,现在由于我试图为每个用户保存一组图像,我不确定如何为一个ID存储一组图像。这是如何做到的?目前我的画廊数据库有两个字段,用户和galleryimage,但我认为这应该成为galleryimage,因为这是一个选择的图像名称。我的配置文件图像之前只是将文件名位置存储为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; ?>


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

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

另外,当你从数据库中读取它时,你可以使用PHP的explode()方法将它转换成数组。你能给我一个例子吗?在函数putGalleryImage中,在代码前加上这一行:好的-这是否意味着我需要修改我的数据库并在其中添加一个ID字段?然后试试这个-$image=$ID.$file\u data['file\u name']是不是一个好主意;作为标准做法,数据库中需要一个唯一的id字段。跟我来。我有一个网站,当新用户注册时,它会自动在我的图库中创建三个文件夹。其中一个文件夹名为“userid”。里面有两个文件夹——“public”和“private”。现在,当这个用户上传一张图片时,他可以选择公共或私有,我只需将路径设置为“userid/public/”,然后用当前时间戳重命名上传,以确保没有同名图片。你不应该使用$file_data['file_name'],如果用户有两张同名图片怎么办。始终使用唯一的名称重命名图片。当前时间戳很好,因为您也可以使用它来从新到旧订购图片,所以我的理解是在我的gallery db中创建一个新的Id字段,然后我需要在我的putGalleryimages函数中更改什么-谢谢您的帮助