[英]Codeigniter -> Image Uploading Part 2 -> Multiple Images
I am trying to create a multiple image uploader and I have come across this link .我正在尝试创建一个多图像上传器,我遇到了这个链接。 What I am confused about in relation to my code below and the link is that do I have to have 2
我对下面的代码感到困惑,链接是我必须有 2
$this->upload->do_upload();
functions to run my code or how do I use运行我的代码的功能或我如何使用
$this->upload->initialize($config);
in the below situation?在下面的情况?
Code:代码:
//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
You need a loop to reinitialize the file upload library so that you can process some other images that are uploaded by the user.您需要一个循环来重新初始化文件上传库,以便您可以处理用户上传的一些其他图像。
Let's say a user uploaded 2 images.假设用户上传了 2 张图片。 Then that means you need to put the code that initialize the file upload library and do the file upload inside that loop.
那么这意味着您需要将初始化文件上传库的代码放入该循环内并进行文件上传。
for ($i = 0; $i < 2; $i++)
{
// Change the config here if necessary
$this->upload->initialize($config);
// Call do_upload() here
}
Kemal is right: you have to iterate over the files you have. Kemal 是对的:您必须遍历您拥有的文件。 I would put the "concepts" in an array, so you can use
foreach
:我会把“概念”放在一个数组中,这样你就可以使用
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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.