【问题标题】:Codeigniter 2.1 - form upload same document twiceCodeigniter 2.1 - 表单上传相同的文档两次
【发布时间】:2013-02-19 16:41:47
【问题描述】:

文件上传有点问题。我正在尝试上传两个文件,但由于某种原因,第一个文件被上传了两次,并且没有第二个文件的踪迹。我做错了什么?

文件上传功能(模型):

public function file_upload($folder, $allowed_type, $max_size = 0, $max_width = 0, $max_height = 0)
{
    $folder = $this->path . $folder;

    $files = array();
    $count = 0;

    foreach ($_FILES as $key => $value) :
        $file_name = is_array($value['name']) ? $value['name'][$count] : $value['name'];
        $file_name = $this->global_functions->char_replace($file_name, '_');
            $count++;
            $config = array(
            'allowed_types' => $allowed_type,
            'upload_path'   => $folder,
            'file_name'     => $file_name,
            'max_size'      => $max_size,
            'max_width'     => $max_width,
            'max_height'    => $max_height,
            'remove_spaces' => TRUE
             );

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

        if (!$this->upload->do_upload($key)) :
            $error = array('error' => $this->upload->display_errors());
            return FALSE;
        else :
            $file = $this->upload->data();
            $files[] = $file['file_name'];
        endif;

    endforeach;
    if(empty($files)):
        return FALSE;
    else:
        return implode(',', $files);
    endif;
}

函数表单文件上传(控制器):

public function dokument_insert()
{
    $this->admin_login_check();

    $dokument =explode(',', $this->doc->file_upload('/doc', 'pdf|PDF|doc|docx') );
    var_dump($dokument);
    $data = array(
        'naziv'         => $this->input->post('naziv_srb'),
        'opis'          => $this->input->post('opis_srb'),
        'path_pdf'      => $dokument[0],
        'path_doc'      => $dokument[1],
        'kategorija_id' => $this->input->post('kategorija'),
        'jezik_id'      => $this->input->post('jezik')
    );
    $this->doc->save($data);
}

文件的部分表单(视图):

<label for="dokument_pdf">Dokument PDF</label>
<input type="file" name="pdf" id="dokument_pdf">

<label for="dokument_doc">Dokument DOC</label>
<input type="file" name="doc" id="dokument_doc">

【问题讨论】:

    标签: php codeigniter codeigniter-2


    【解决方案1】:

    您必须在每次上传时初始化上传库。

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

    用户指南:https://www.codeigniter.com/user_guide/libraries/file_uploading.html

    【讨论】:

      【解决方案2】:

      看起来因为你有 2 个文档字段,它无论如何都会循环 2 次......

      如果您只想要一个,您应该通过表单只发送一个,或者不要使用 ForEach 并使用它们的固定名称手动处理每个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-08
        • 1970-01-01
        • 2012-09-09
        • 1970-01-01
        • 2013-07-13
        • 2012-01-30
        • 2020-07-31
        相关资源
        最近更新 更多