1

我目前正在尝试将图像上传到页面并将它们保存在 2 个文件夹中。应该发生的是我上传了一张图片,它保存在我的画廊和拇指文件夹中。

然后保存在拇指文件夹中的图像将显示在屏幕上,当单击图像时,它将以适当的大小显示,就像它保存在画廊文件夹中一样。目前,图像已保存到图库数据库和我的图库文件夹,但拇指图像未显示在屏幕上。

我认为是因为它没有保存到拇指文件夹中。为什么没有保存到文件夹中?

控制器:

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'));
      $this->gallery_path = 'web-project-jb/assets/gallery/';
      $this->gallery_path_url = base_url().'web-project-jb/assets/gallery/';
   }

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

      $this->load->library('upload', $config);

      $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();

      $username = $this->session->userdata('username');

      if ( ! $this->upload->do_upload()) {

         $error = array('error' => $this->upload->display_errors());
         $username = $this->session->userdata('username');
         $viewData['username'] = $username;

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

      } else {

         $file_data  = $this->upload->data();
         $image = $this->gallery_path.$file_data['file_name'];
         $data['image'] = $this->gallery_path.$file_data['file_name'];
         $this->username = $this->session->userdata('username');
         $images = $this->session->userdata('images');
         $data['images'] = $images;
         $this->gal_model->putGalleryImage($username, $image);
         $this->session->set_userdata($image);
         $viewData['username'] = $username;
         $data['gal_model'] = $this->gal_model->get_images($username);
         var_dump($image);

         $username = $this->session->userdata('username');

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

   function index() {
      $username = $this->session->userdata('username');
      $images = $this->session->userdata('images');
      $this->load->library('upload');
      $data['gal_model'] = $this->gal_model->get_images($username);
      $file_data = $this->upload->data();
      $file_data['file_name'] = $this->gal_model->get_images($username);
      $image = $this->gallery_path.$file_data['file_name'];
      $data['image'] = $file_data['file_name'];
      $data['images'] = $images;
      $viewData['username'] = $username;

      $this->load->view('shared/header');
      $this->load->view('gallery/galtitle', $viewData);
      $this->load->view('shared/nav');
      $this->load->view('gallery/galview', $viewData, $data, array('error' => ' '));
      $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;
    } else {
      return false;
    }
  }
  function putGalleryImage($username, $image) {
    $record = array('user' => $username, 'galleryimage' => $image);
    $this->session->set_userdata($image);
    if ($this->exists($username)) {
      $this->db->where('user', $username)->insert('gallery', $record);
    }
  }
  function get_images($username) {
    $this->db->select('*')->from('gal_model')->where('user', $username);
    $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;
  }
}

画廊视图:

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

0 回答 0