使用unlink CodeIgniter更新Iage

时间:2016-07-04 12:21:04

标签: php codeigniter-3

我有关于“取消关联”更新的问题。error message after post

控制器

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Picture_controller extends CI_Controller 
{

    public function index(){
        $result = $this->db->query("SELECT * FROM img");
        $this->load->view('picture/index', array('data'=>$result));
    }

    public function form(){
        $this->load->view('picture/form', array('error' => ''));
    }

    public function edit($id){
        $data = $this->db->query("SELECT * FROM img WHERE id = '{$id}' ");
        $row = $data->row();
        $this->load->view('picture/edit',array('r'=>$row));
    }

    public function do_update(){    
        $id = $this->input->post('id');
        $path = $this->input->post('path');
        if (isset($_FILES['userfile']['name']) && !empty($_FILES['userfile']['name'])) 
        {
            if(unlink('uploads/'.$path))
            {
                $config['upload_path'] = './uploads';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = 20000000;
                $config['max_width'] = 1024;
                $config['max_height'] = 768;
                $config['encrypt_name'] = TRUE;

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

                if(!$this->upload->do_upload()) 
                {
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('picture/form', $error);
                }
                else
                {
                    $data = $this->upload->data();
                    // var_dump($data);
                    $data_array = array(
                        'file_name' => $data['file_name'],
                        'original_name' => $data['orig_name'],
                        'file_size' => $data['file_size'],
                        'file_ext' => $data['file_ext'],
                        'full_path' => $data['full_path']
                        );
                $this->db->where('id',$id);
                $this->db->update('img',$data_array);
                redirect('Picture_controller/index');
                }
            }
        }
        else
        {
            redirect('Picture_controller/index');
        }
    }

    public function do_upload()
    {

        $config['upload_path'] = './uploads';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 20000000;
        $config['max_width'] = 1024;
        $config['max_height'] = 768;
        $config['encrypt_name'] = TRUE;


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

    if(!$this->upload->do_upload()) 
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('picture/form', $error);
    }
    else
    {
        $data = $this->upload->data();
        // var_dump($data);
        $data_array = array(
            'file_name' => $data['file_name'],
            'original_name' => $data['orig_name'],
            'file_size' => $data['file_size'],
            'file_ext' => $data['file_ext'],
            'full_path' => $data['full_path']
            );
    $this->db->insert('img',$data_array);
    redirect('Picture_controller/index');
    }
}

}
?>

有什么问题?你能帮我吗?

1 个答案:

答案 0 :(得分:0)

$path变量未定义(我猜$_POST没有path键)因此脚本会尝试删除uploads/(整个目录)。< / p>