0

我正在尝试创建一个多图像上传器,我遇到了这个链接。我对下面的代码感到困惑,链接是我必须有 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
4

2 回答 2

4

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

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

for ($i = 0; $i < 2; $i++)
{
     // Change the config here if necessary
     $this->upload->initialize($config);
     // Call do_upload() here
}
于 2012-04-11T04:55:54.187 回答
1

Kemal 是对的:您必须遍历您拥有的文件。我会将“概念”放在一个数组中,这样您就可以使用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');
于 2012-04-11T05:11:05.813 回答