Codeigniter 2.1 - 表格上传同一文件两次

时间:2013-02-19 16:41:48

标签: php codeigniter codeigniter-2

我有一些文件上传的问题。我试图上传两个文件,但由于某种原因,第一个文件被上传两次,没有第二个文件的痕迹。我做错了什么?

文件上传功能(型号):

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

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

看起来因为你有两个doc字段,它会循环2次,无论......

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