1

我试图将几个文件与 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);
                }
            }

但是当我运行代码时,我注意到它不验证 if 条件,它会留下它们。在 CI 示例中,我注意到他们不使用 post 数组来获取名称,而是直接使用硬编码。但这在我的应用程序中是不可能的。

如何解决这个问题。我认为问题出在这一行$this->input->post('emp_nic_attachment')

4

2 回答 2

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;
}
于 2012-12-24T06:24:55.163 回答
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 库。这是无用的。

于 2012-12-24T11:55:58.037 回答