有表格,用户输入全名,年龄,个人资料图片,文件并提交。我设法让表单使用一个文件上传,但我找不到从2个不同输入上传文件的正确方法。
以下是我的表格
HTML
<form enctype="multipart/form-data" action="" method="post">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="full_name"/>
</div>
<div class="form-group">
<label>Age</label>
<input type="text" class="form-control" name="age"/>
</div>
<div class="form-group">
<label>Choose Profile pic</label>
<input type="file" class="form-control" name="userfile1"/>
</div>
<div class="form-group">
<label>Choose Document</label>
<input type="file" class="form-control" name="userfile2"/>
</div>
<div class="form-group">
<input class="form-control" type="submit" name="upload" value="UPLOAD"/>
</div>
</form>
PHP
if ($this->input->post('upload'))
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf';
$config['max_size'] = 10000;
$config['max_width'] = 3000;
$config['max_height'] = 3000;
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile1'))
{
$this->session->set_flashdata('status','<div class="alert alert-danger alert-style-square">
<p style="text-align:center">There was an error. Try again. '.$this->upload->display_errors().'</p>
</div>');
}
else
{
$data = array('upload_data' => $this->upload->data());
$values = array('full_name'=>$this->input->post('full_name'),
'age'=>$this->input->post('age'),
'profile_image'=>$this->upload->data('file_name'),
//'document'=>$this->upload->data('file_name'),
'uploaded_date'=>date("Y-m-d H:i:s"));
var_dump($values);
}
}
我们非常感谢您的帮助和指南
答案 0 :(得分:0)
您可以尝试更改
if ( !$this->upload->do_upload('userfile1'))
到
if ( !$this->upload->do_upload('userfile1') AND !$this->upload->do_upload('userfile2'))
执行此操作将要求用户上传两个图像。如果只需要一张图片,您可以尝试将 AND 更改为 OR 。