【问题标题】:Upload Query Codeigniter上传查询 Codeigniter
【发布时间】:2011-04-05 04:14:36
【问题描述】:

我已经完成了下面的代码,现在我得到一个数据库错误 -> 似乎仍然无法上传:

错误:

Column 'image_path' cannot be null

Database Structure

控制器:

class Addsale extends CI_Controller {

function __construct(){
parent::__construct();
}
function index() {
if(!$this->session->userdata('logged_in')) {
    redirect('admin/home');
}
// Main Page Data
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Add Sale';
$data['content'] = $this->load->view('admin/addsale', $data);

$this->load->view('admintemplate', $data);

//Set Validation
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|is_natural|required');
$this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim|required');
$this->form_validation->set_rules('condition', 'Condition', 'trim|required');
$this->form_validation->set_rules('description', 'Description', 'trim|required');
$this->form_validation->set_rules('price', 'Price', 'trim|required');

if($this->form_validation->run() === TRUE) {
$this->load->library('upload', $config);
$file_info = $this->upload->do_upload();
$data = array(  
    'name' => $this->input->post('name', TRUE),
    'location' => $this->input->post('location', TRUE),
    'bedrooms' => $this->input->post('bedrooms', TRUE),
    'bathrooms' => $this->input->post('bathrooms', TRUE),
    'condition' => $this->input->post('condition', TRUE),
    'description' => $this->input->post('description', TRUE),
    'price' => $this->input->post('price', TRUE),
    'image_path' => $file_info['full_path']
    );
$this->sales_model->addSale($data);
   }
}

function do_upload(){

    //Set File Settings
    $config['upload_path'] = './includes/uploads/';
    $config['allowed_types'] = 'jpg|png';
    $config['max_size'] = '100';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';


}


}

型号:

    function addSale($data) {

$this->db->insert('sales', $data);
return;
}   

原文代码:

你好,

我有以下代码段:

if($this->form_validation->run() === TRUE) {    
      $data = array(  
        'name' => $this->input->post('name', TRUE),
        'location' => $this->input->post('location', TRUE),
        'bedrooms' => $this->input->post('bedrooms', TRUE),
        'bathrooms' => $this->input->post('bathrooms', TRUE),
        'condition' => $this->input->post('condition', TRUE),
        'description' => $this->input->post('description', TRUE),
        'price' => $this->input->post('price', TRUE),
        'image_path' => $this->input->post('userfile', TRUE)
        );
    $this->load->library('upload', $config);
    $this->upload->do_upload(); 
       }
    }

我要做的是当表单有效时将数据保存到数据库(工作正常)并上传图像。

如果我在苦苦挣扎,是因为我无法保存用户文件“名称”并且文件不会上传。

这可以在索引函数中完成吗?

【问题讨论】:

  • 我没有,但没有一个对我有帮助,大部分都偏离了轨道

标签: codeigniter upload


【解决方案1】:

上传的文件根本不会包含在帖子中,因此您无法以您想要的方式获得名称。你必须这样做:

if($this->form_validation->run() === TRUE) {    
    $this->load->library('upload', $config);
    $file_info = $this->upload->do_upload(); 

    $data = array(  
        'name' => $this->input->post('name', TRUE),
        'location' => $this->input->post('location', TRUE),
        'bedrooms' => $this->input->post('bedrooms', TRUE),
        'bathrooms' => $this->input->post('bathrooms', TRUE),
        'condition' => $this->input->post('condition', TRUE),
        'description' => $this->input->post('description', TRUE),
        'price' => $this->input->post('price', TRUE),
        'image_path' => $file_info['full_path']
    );
}

如果您只希望文件名使用“client_name”或其他名称,这会将完整路径放入您的数据库中(请参阅罗斯的答案以供参考)

【讨论】:

    【解决方案2】:

    函数$this->upload->do_upload()返回一个关于上传文件的信息数组。

    所以:

    $upload_data = $this->upload->do_upload();
    print_r($upload_data); // to see everything
    
    Array
    (
        [file_name]    => mypic.jpg
        [file_type]    => image/jpeg
        [file_path]    => /path/to/your/upload/
        [full_path]    => /path/to/your/upload/jpg.jpg
        [raw_name]     => mypic
        [orig_name]    => mypic.jpg
        [client_name]  => mypic.jpg
        [file_ext]     => .jpg
        [file_size]    => 22.2
        [is_image]     => 1
        [image_width]  => 800
        [image_height] => 600
        [image_type]   => jpeg
        [image_size_str] => width="800" height="200"
    )
    

    因此,根据您需要的值,您需要先进行上传,如果成功,则继续处理 POST 数据。然后你可以在你的 $data 数组中这样做:

    'image_path' => $upload_data['file_name']; // adjust value to get what you want.

    你应该很高兴。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      相关资源
      最近更新 更多