Codeigniter - >图像上传第2部分 - >多个图像

时间:2012-04-11 04:36:08

标签: php image codeigniter image-upload

我正在尝试创建一个多图像上传器,但我遇到了这个link。我对下面的代码和链接的困惑是我必须要有2个

$this->upload->do_upload();用于运行我的代码或如何使用

$this->upload->initialize($config);在下面的情况?

代码:

//Image Upload Function

$conceptOne = 'conceptOne';
$conceptTwo = 'conceptTwo';

$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';

$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);

if(!is_dir($location.$folderName))
{   
    mkdir($location.$folderName);
    chmod($location.$folderName, 0777);

    //Set File Settings 
    $config['upload_path'] = $location.$folderName; 
    $config['allowed_types'] = 'jpg|png|pdf'; 
    $config['file_name'] = $conceptOne;
    $config['remove_spaces'] = TRUE;
    $config['overwrite'] = TRUE;
    $config['max_size'] = '1024'; 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 

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

    print_r($config);

    if(!$this->upload->do_upload($conceptOne)) { #= try upload
        $data['uploadError'] = array('uploadError' => $this->upload->display_errors()); #Error
        $this->load->view('layout', $data);
    } // Do upload
    else{
        $data = array('upload_data' => $this->upload->data($conceptOne));
    }// end else
}// end if folder

2 个答案:

答案 0 :(得分:4)

您需要一个循环来重新初始化文件上传库,以便您可以处理用户上传的其他一些图像。

假设用户上传了2张图片。那意味着你需要放置初始化文件上传库的代码并在该循环中进行文件上传。

for ($i = 0; $i < 2; $i++)
{
     // Change the config here if necessary
     $this->upload->initialize($config);
     // Call do_upload() here
}

答案 1 :(得分:1)

凯末尔是对的:你必须遍历你拥有的文件。我会将“概念”放在数组中,因此您可以使用foreach

// Load upload library without any configuration
$this->load->library('upload');

$concepts = array('conceptOne','conceptTwo');

$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';

$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);

if(!is_dir($location.$folderName))
{   
    mkdir($location.$folderName);
    chmod($location.$folderName, 0777);
}

$config['upload_path'] = $location.$folderName;
$config['allowed_types'] = 'jpg|png|pdf';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';

// Upload 'concepts'
foreach($concepts as $concept)
{
    $config['file_name'] = $concept;
    $this->upload->initialize($config);
    $this->upload->do_upload($concept);
}

// Upload logo
$config['file_name'] = 'logo-filename.gif';
$this->upload->initialize($config);
$this->upload->do_upload('logo');