CodeIgniter多文件上传

时间:2011-11-01 18:28:44

标签: php codeigniter

首先,我要求如何使用CodeIgniter执行多文件上传,而是如何防止上传任何/所有文件最后一行失败(由于文件类型,大小等)。基本上我希望预先使用CI的文件验证(在上传任何内容之前),如果一切看起来都很好,请执行所有上传。

我有一个上传两个文件的表单。如果第一个文件通过验证并且do_upload()方法返回TRUE,但第二个文件失败,则第一个文件仍然上传 - 我该如何防止这种情况?

这是我的代码:

// Controller

function upload_files()
{

    // Configure first file upload  
    $config['upload_path']          = 'public/mp3/';
    $config['overwrite']            = FALSE;
    $config['allowed_types']        = 'mp3';
    $config['max_size']             = '20000';
    $config['remove_spaces']        = TRUE;

    $this->upload->initialize($config);

    // Perform first file upload
    if ($this->upload->do_upload('mp3'))
    {
        // Store upload file info for later use
        $mp3_info = $this->upload->data();

        // Configure second file upload
        $config['upload_path']          = 'public/doc/';
        $config['overwrite']            = FALSE;
        $config['allowed_types']        = 'doc|docx';
        $config['max_size']             = '3000';
        $config['remove_spaces']        = TRUE;
        $this->upload->initialize($config);

        // Perform second file upload
        // Even if this fails, the first file was still uploaded
        if ($this->upload->do_upload('doc'))
        {
            // Store upload file info for later use
            $doc_info = $this->upload->data();

            // Perform database interactions here
        }
        else
        {
            echo $this->upload->display_errors();
        }
    }
    else
    {
        echo $this->upload->display_errors();
    }
}

谢谢!

1 个答案:

答案 0 :(得分:0)

没有骰子!除非您执行上传,否则无法验证来自php或codeigniter的上传。 php如何验证它还无法访问的内容?

您必须使用javascript预先进行一些验证。如果所有文件都通过js验证,则上传文件并使用php / codeigniter完成验证。