我试图将多个文件与CI框架一起上传。我想要的是将文件上传到不同的文件夹。因此,我尝试检查文件输入元素的名称并设置填充路径。
以下代码可以清楚地了解我的问题。
$config['allowed_types'] = 'jpg|doc|pdf|docx';
//$config['max_size'] = '100';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
if($this->input->post('bankbook_attachment')){
echo 'bank'.$this->input->post('bankbook_attachment');
$config['upload_path'] = './uploads/bank/';
$fillselect='bankbook_attachment';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($fillselect))
{
$error = array('error' => $this->upload->display_errors());
echo 'error'.print_r($error);
}
else
{
// $data = array('upload_data' => $this->upload->data());
// print_r($data);
$datame=$this->upload->data();
$fillnamebank= $datame['file_name'];
echo 'fillname is'.$fillnamebank;
// $this->load->view('upload_success', $data);
}
}
if($this->input->post('emp_nic_attachment')){
$config['upload_path'] = './uploads/nic/';
$fillselect='emp_nic_attachment';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($fillselect))
{
$error = array('error' => $this->upload->display_errors());
echo 'error'.print_r($error);
}
else
{
// $data = array('upload_data' => $this->upload->data());
// print_r($data);
$datame=$this->upload->data();
$fillnamenic= $datame['file_name'];
echo 'fillname is'.$fillnamenic;
// $this->load->view('upload_success', $data);
}
}
但是当我运行代码时,我注意到它不会验证条件,它会离开它们。 在CI示例中,我注意到他们不使用post数组来获取名称,而是直接使用硬编码。但是在我的申请中这是不可能的。
如何解决这个问题。我认为问题就在于这一行$this->input->post('emp_nic_attachment')
答案 0 :(得分:1)
由于您要更新$config
中的上传目录,我认为您不必检查'填充名称'。下面的代码就足够了
$config['upload_path'] = './uploads/bank/';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('bankbook_attachment'))
{
$error = array('error' => $this->upload->display_errors());
echo 'error'.print_r($error);
}
else
{
$datame=$this->upload->data();
$fillnamebank= $datame['file_name'];
echo 'fillname is'.$fillnamebank;
}
$config['upload_path'] = './uploads/nic/';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('emp_nic_attachment'))
{
$error = array('error' => $this->upload->display_errors());
echo 'error'.print_r($error);
}
else
{
$datame=$this->upload->data();
$fillnamenic= $datame['file_name'];
echo 'fillname is'.$fillnamenic;
}
答案 1 :(得分:0)
在花了几个小时和论坛帮助后,我可以找到答案。 简单而重要的概念是我们每次进行多次上传时都要加载上传库和配置文件。
$config['upload_path'] = './uploads/bank/';
$config['allowed_types'] = 'doc|docx|pdf|jpg';
$fillselect='bankbook_attachment';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($fillselect))
{
$error = array('error' => $this->upload->display_errors());
echo 'error'.print_r($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
// print_r($data);
$datame=$this->upload->data();
$fillnamebank= $datame['file_name'];
echo 'fillname is'.$fillnamebank;
// $this->load->view('upload_success', $data);
}
$config['upload_path'] = './uploads/nic/';
$config['allowed_types'] = 'doc|docx|pdf|jpg';
$fillselect2='emp_nic_attachment';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($fillselect2))
{
$error = array('error' => $this->upload->display_errors());
echo 'error'.print_r($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
// print_r($data);
$datame=$this->upload->data();
$fillnamenic= $datame['file_name'];
echo 'fillname is'.$fillnamenic;
// $this->load->view('upload_success', $data);
}
我做的另一个错误是在构造函数中调用uplod库。这是用途。