删除不在代码点火器图库中工作的图像

时间:2014-01-04 10:30:49

标签: php codeigniter

即使在更改路径后上传也能正常工作,但我看不到删除方法有什么变化?

class Image extends CI_Controller {

private $data = array();

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

private function assignUsername($username)
{
    $this->data = array(

                    'dir' => array(
                            "original" => "assets/uploads/original/users/".$username."/",
                            "thumb" => "assets/uploads/thumbs/users/".$username."/"
                    ),
                    'total' => 0,
                    'images' => array(),
                    'error' => ''
            );
}

public function index($start = 0)
{
    $user = $this->session->userdata('username');

    $originalpath = "assets/uploads/original/users/".$user."/";
    $thumbpath = "assets/uploads/thumbs/users/".$user."/";

    if(!is_dir($originalpath)) //create the folder if it's not already exists
    {
        mkdir($originalpath,0755,TRUE);
        mkdir($thumbpath,0755,TRUE);
    }

    $this->assignUsername($user);

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

    $this->load->library('pagination');

    $c_paginate['base_url'] = site_url('image/index');
    $c_paginate['per_page'] = '5';
    $finish = $start + $c_paginate['per_page'];

    if (is_dir($this->data['dir']['thumb']))
    {
        $i = 0;
        if ($dh = opendir($this->data['dir']['thumb'])) {
            while (($file = readdir($dh)) !== false) {
                // get file extension
                $ext = strrev(strstr(strrev($file), ".", TRUE));
                if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png') {
                    if ($start <= $this->data['total'] && $this->data['total'] < $finish) {
                        $this->data['images'][$i]['thumb'] = $file;
                        $this->data['images'][$i]['original'] = str_replace('thumb_', '', $file);
                        $i++;
                    }
                    $this->data['total']++;
                }
            }
            closedir($dh);
        }
    }

    $c_paginate['total_rows'] = $this->data['total'];

    $this->pagination->initialize($c_paginate);

    $this->load->view('imagesTest/indexTest', $this->data);
}

private function upload()
{
    $c_upload['upload_path']    = $this->data['dir']['original'];
    $c_upload['allowed_types']  = 'gif|jpg|png|jpeg|x-png';
    $c_upload['max_size']       = '100';
    $c_upload['max_width']      = '1024';
    $c_upload['max_height']     = '768';
    $c_upload['remove_spaces']  = TRUE;

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

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

        $img = $this->upload->data();

        // create thumbnail
        $new_image = $this->data['dir']['thumb'].'thumb_'.$img['file_name'];

        $c_img_lib = array(
            'image_library'     => 'gd2',
            'source_image'      => $img['full_path'],
            'maintain_ratio'    => TRUE,
            'width'             => 100,
            'height'            => 100,
            'new_image'         => $new_image
        );

        $this->load->library('image_lib', $c_img_lib);
        $this->image_lib->resize();
    } else {
        $this->data['error'] = $this->upload->display_errors();
    }
}

public function delete($ori_img)
{


    unlink($this->data['dir']['original'].$ori_img);
    unlink($this->data['dir']['thumb'].'thumb_'.$ori_img);
    redirect('/imageTest');
}
}

/ *文件结尾image.php / / 位置:./ system / application / controllers / image.php * /

1 个答案:

答案 0 :(得分:0)

没有分配$this->data。 试试:

public function delete($ori_img)
{
    $user = $this->session->userdata('username');
    $this->assignUsername($user);
    unlink($this->data['dir']['original'].$ori_img);
    unlink($this->data['dir']['thumb'].'thumb_'.$ori_img);
    redirect('/imageTest');
}