Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将图像名称上载控制器发送到resister controller_Php_Codeigniter - Fatal编程技术网

Php 如何将图像名称上载控制器发送到resister controller

Php 如何将图像名称上载控制器发送到resister controller,php,codeigniter,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

视图代码

<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>

只需从upload函数调用register函数并发送图像名称即可

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);
    }
}
函数上传()
{
$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);
如果(!$this->upload->do_upload())
{
echo“文件无效/文件未选择”;
$error=array('error'=>$this->upload->display_errors());
}
其他的
{
$data=array('upload_data'=>$this->upload->data());
$img_name=$data['upload_data']['file_name'];
回声“'”;
//将图像名称发送到注册函数
注册(img_名称);
}
}
功能寄存器($img\u名称)
{
$this->form_validation->set_规则('name'、'name'、'trim | required');
$this->form_validation->set_规则('city'、'city'、'trim | required');
$this->form_validation->set_规则('mobile'、'mobile'、'trim | required');
如果($this->form\u validation->run()==FALSE)
{
}
其他的
{
//将图像名称和表单数据发送到模型
$data=$this->register\u model->register($img\u name,$form\u data);
}
}

我认为ajaxform提交无法正常上传文件。必须使用Formdata()对象 请查收

注册控制器或方法?我认为您正在使用注册方法。。。为什么要使用两个函数?只需将注册码保留在上传功能中,我就可以使用Ajax上传图像并立即显示,所以我不能只使用resister控制器,因为同时图像不能只使用resister控制器显示并将数据插入MySQL数据库。首先,我想上传图像并立即显示,然后我想填写输入字段并提交。现在,所有数据都将被插入,并且还会在resister表中插入图像_名称。请您给我任何建议。请再次查看我的代码(我已经编辑了一些),在第一个表单中,使用Ajax上传图像后,在第二个表单中填写输入字段时,单击提交按钮,然后点击注册控制器。现在,我将如何将图像名称上传控制器发送到resister controller,并将所有具有图像名称的数据插入resister表中。根据您的建议,如果我使用您的代码,则会看到以下严重性:警告消息:Setup_con::resister()缺少参数1文件名:controllers/setup_con.php行号:2571)打开图像上传表单2)将图像上传到上传控制器3)从上传函数调用注册函数并发送图像4)现在您使用图像注册函数。现在调用第二个表单并获取数据。。现在您将拥有图像和表单数据
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);
    }
}