如何将图像名称上传控制器发送到注册控制器

时间:2012-12-08 07:09:35

标签: php codeigniter

查看代码

<form id="form1" method="post" enctype="multipart/form-data" action='<?php echo base_url();?>index.php/setup_con/upload'>

<input type="file" name="userfile" id="userfile" required="required"/>
</form>

<form id="form2" method="post"  enctype="multipart/form-data" action='<?php echo base_url();?>index.php/setup_con/register'>
<input type="text" name="name" id="name"/> 
<input type="text" name="city" id="city"/> 
<input type="text" name="mobile" id="mobile"/> 
<input type="submit" value="Submit" />
</form>

控制器代码

function upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '800';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

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

    if (!$this->upload->do_upload())
    {
        echo "File is Not valid/File does not select";
        $error = array('error' => $this->upload->display_errors());

    }
    else
    {

        $data = array('upload_data' => $this->upload->data());

        $img_name =  $data ['upload_data']['file_name'];

            echo "'<img src='".base_url()."uploads/".$img_name."' class='preview'>";

    }
}
    function register()
    {
        $this->form_validation->set_rules('name','Name', 'trim|required');
        $this->form_validation->set_rules('city','city', 'trim|required');
        $this->form_validation->set_rules('mobile','mobile', 'trim|required');
        if($this->form_validation->run() == FALSE)
    {
        }
        else
        {
           $data = $this->register_model->register();
        }
    }

我在第一个表格中使用ajax上传图像,在codeigniter中控制器上传,这里只上传图像这个控制器但我想图像名称发送寄存器控制器,我将插入image_name,名称,城市和monbile in电阻表。

请帮助我,

我怎样才能成功

2 个答案:

答案 0 :(得分:0)

只需从上传功能调用注册功能并发送图像名称。

function upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '800';
$config['max_width']  = '1024';
$config['max_height']  = '768';

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

if (!$this->upload->do_upload())
{
    echo "File is Not valid/File does not select";
    $error = array('error' => $this->upload->display_errors());

}
else
{

    $data = array('upload_data' => $this->upload->data());

    $img_name =  $data ['upload_data']['file_name'];

        echo "'<img src='".base_url()."uploads/".$img_name."' class='preview'>";
        //Send image name to register function
        register($img_name);

}
}
function register($img_name)
{
    $this->form_validation->set_rules('name','Name', 'trim|required');
    $this->form_validation->set_rules('city','city', 'trim|required');
    $this->form_validation->set_rules('mobile','mobile', 'trim|required');
    if($this->form_validation->run() == FALSE)
{
    }
    else
    {
       // Send image name and form data to the model
       $data = $this->register_model->register($img_name,$form_data);
    }
}

答案 1 :(得分:0)

我认为ajaxform提交无法正常上传文件。您必须使用Formdata()对象 请检查  How to send FormData objects with Ajax-requests in jQuery?