0

这就是我在codeigniter中尝试做的事情:

我的会员资料页面都有自己的照片库。用户能够登录和上传照片。我希望将照片放在带有用户名的文件夹中。因此,如果用户不存在该目录,我希望在他上传照片后创建它。最后一件事是我希望上传的照片与该用户相关并显示在他的个人资料页面上。

我有什么工作:

我可以上传照片,但它位于常规上传文件夹中,我还可以在数据库中看到与该照片相关的文件名,并且可以查看文件。

我需要在带有用户名或 ID 的文件夹中创建用户照片。并且只有用户照片才能显示在他的页面上。这是我的画廊模型:

<?php

class Gallery_model extends CI_Model{
    // initalizes the gallery path variable
    var $gallery_path;
    var $gallery_path_url;
    public function __construct(){
        parent::__construct();
        //sets the gallery path to applications folder and gallery image path
        $this->gallery_path = realpath(APPPATH . '../portfolio');
        $this->gallery_path_url = base_url().'portfolio/';
    }

    function do_upload(){

        $config = array(
            // requried variable allowed types is needed also delcared the upload path to gallery path
            'allowed_types' => 'jpg|jpeg|gif|png',
            'upload_path' => $this->gallery_path,
            'max_size' => 3000
        );

        // loads the library and sets where its going to go to
        $this->load->library('upload',$config);
        // this performs the upload operation

        $this->upload->do_upload();
        //returns data about upload ( file location )
        $image_data = $this->upload->data();

        $config = array(
            'source_image' => $image_data['full_path'],
            'new_image' => $this->gallery_path . '/thumbs',
            'maintain_ration' => true,
            'width' => 250,
            'height' => 220
        );



        $data = array(
          'username' => $this->input->post('username'),
          'category' => $this->input->post('category'),
          'filename' => $image_data['file_name'],
        );

        $this->db->insert('portfolio', $data );

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

    }
    function get_images(){

        $files = scandir($this->gallery_path);
        // substracts these out of array
        $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;
    }
}

这是我的观点:

        function gallery(){
        $this->load->model('user_model');
        $data = array(
            //$session_id = $this->session->userdata('session_id')
        ); // if no data is there and you wont get an error
            if($query = $this->user_model->get_profile())
        {
            $data['records'] = $query;


        }
        // loads the model. if the upload posts, call the do model method from the gallery model Model.
        $this->load->model('gallery_model');
        if ($this->input->post('upload')){
            $this->gallery_model->do_upload();
        }
        // displays the images
        $data['images'] = $this->gallery_model->get_images();

        $data['main_content'] = '/pages/gallery';
        $this->load->view('templates/template',$data);
    }
4

1 回答 1

0

您会在投资组合文件夹中看到原始文件名,因为您错过了上传库的“file_name”配置变量。例子:

$config = array(
  // requried variable allowed types is needed also delcared the upload path to gallery path
    'allowed_types' => 'jpg|jpeg|gif|png',
    'upload_path' => $this->gallery_path,
    'max_size' => 3000,
    'file_name' => "the user id or username"
);

我还看到您没有设置“覆盖”变量。可能是一个问题,因为您将文件名存储为用户名,因为当用户上传新照片时 CI 不会覆盖文件。

于 2013-07-27T05:26:16.913 回答