【问题标题】:Upload Confusion -> Uploading file and saving image_path to dbUpload Confusion -> Uploading file and save image_path to db
【发布时间】:2011-07-25 19:38:04
【问题描述】:

对于附加此问题的最佳方法,我有点困惑 - 我有一个要添加到数据库的表单,但现在我想上传一张图片。

图片是ment到./includes/uploads/,文件名是ment要插入到image_path

控制器

class Addsale extends CI_Controller {

function __construct(){
parent::__construct();
}
function index() {
    if(!$this->session->userdata('logged_in')) {
        redirect('admin/home');
    }
    // Set Data
    $data['title'] = "Add Sale";
    $data['sales_pages'] = $this->sales_model->getSalesPages();
    $data['cms_pages'] = $this->navigation_model->getCMSPages();

    //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';
    $this->upload->initialize($config);

    //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) {

    $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('image', TRUE)
        );
        $data = array('image_path' => $this->upload->data());
        $this->sales_model->addSale($data);

        redirect('admin/addsale' , $data);

        $this->session->set_flashdata('success', 'Page Saved');

    }else{
        $data['content'] = $this->load->view('admin/addsale', NULL, TRUE);
        $this->load->view('template', $data);
        }

}


}   

查看

<?php
//Setting form attributes
$formAddSale = array('id' => 'addSale', 'name' => 'addSale');
$saleName = array('id' => 'name', 'name' => 'name');
$saleLocation = array('id' => 'location', 'name' => 'location');
$saleBedrooms = array('id' => 'bedrooms','name' => 'bedrooms');
$saleBathrooms = array('id' => 'bathrooms','name' => 'bathrooms');
$saleCondition = array('id' => 'condition','name' => 'condition');
$saleImage = array('id' => 'image', 'name'=> 'image');
$saleDescription = array('id' => 'description','name' => 'description');
$salePrice = array('id' => 'price','name' => 'price');
?>

<section id = "validation"><?php print $this->session->flashdata('success'); ?></section>
<section id = "validation"><?php echo validation_errors();?></section>

<?php
echo form_open_multipart('admin/addsale/', $formAddSale);
echo form_fieldset();
echo form_label('Name:', 'name');
echo form_input($saleName);
echo form_label ('Location', 'location');
echo form_input($saleLocation);
echo form_label ('Bedrooms', 'bedrooms');
echo form_input($saleBedrooms);
echo form_label ('Bathrooms', 'bathrooms');
echo form_input($saleBathrooms);
echo form_label ('Condition', 'condition');
echo form_input($saleCondition);
echo form_label ('Price', 'price');
echo form_input($salePrice);
echo form_label('Image:', 'image');
echo form_upload($saleImage);
echo form_label ('Description', 'description');
echo form_textarea($saleDescription);
echo form_submit('submit','Submit');
echo form_fieldset_close();
echo form_close();
?>

型号

class Sales_model extends CI_Model
  {

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

function getSalesPages() {

        $query = $this->db->get('sales');
        if($query->num_rows() > 0) return $query->result();

    }

function addSale($data) {

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

function updateSale($id, $data) {

    $this ->db->where('id', $id);
    $this->db->update('sales', $data);
}
  }

【问题讨论】:

    标签: forms codeigniter file-upload


    【解决方案1】:

    您实际上从未在控制器中调用过 do_upload 方法。试试:

    function index() {
    if(!$this->session->userdata('logged_in')) {
        redirect('admin/home');
    }
    // Set Data
    $data['title'] = "Add Sale";
    $data['sales_pages'] = $this->sales_model->getSalesPages();
    $data['cms_pages'] = $this->navigation_model->getCMSPages();
    
    //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';
    $this->upload->initialize($config);
    
    //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) {
    
    $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('image', TRUE)
        );
    
        if ( ! $this->upload->do_upload())
        {
            /// Upload Failed
            $error = array('error' => $this->upload->display_errors());
            // Print $error to user
        }
        else
        {
            // Upload Succeeded add the path to the data array
            $uploaded_file = $this->upload->data();
            $data['image_path'] = $uploaded_file['full_path'];
        }
    
        $this->sales_model->addSale($data);
    
        redirect('admin/addsale' , $data);
    
        $this->session->set_flashdata('success', 'Page Saved');
    
    }else{
        $data['content'] = $this->load->view('admin/addsale', NULL, TRUE);
        $this->load->view('template', $data);
        }
    

    }

    我尚未测试此代码,但我从文档中提取了 do_upload 块,因此它应该可以工作。

    【讨论】:

    • 谢谢 Lee,由于 do_upload 是一个函数,这不起作用,我正在尝试在主索引函数中执行此操作。
    • 嗨,Jess,do_upload() 方法是上传类的一部分,而不是您的控制器,它应该可以工作吗?
    【解决方案2】:

    我现在有了这个,这是我的最终代码:

        if($this->form_validation->run() === TRUE) { 
    
    //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'; 
    
    $this->load->library('upload', $config); 
    
    $this->upload->do_upload();
    $file_info = $this->upload->data();
    
    $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' => $file_info['full_path'] 
        ); 
    $this->sales_model->addSale($data); 
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-08
      • 2012-01-10
      • 2022-12-02
      • 2018-11-12
      • 2014-05-28
      • 1970-01-01
      • 2022-12-28
      相关资源
      最近更新 更多