Codeigniter将文件上传到gallery文件夹

时间:2013-07-27 04:53:56

标签: php database codeigniter upload relational

这就是我在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);
    }

1 个答案:

答案 0 :(得分: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不会覆盖该文件。