您可以在 foreach 循环中调用 Codigniter 的 do_upload() 函数,该循环遍历您发布的文件数组$_FILE
;
这是用于接收上传表单 POST 的控制器函数中的代码:
$this->load->library('upload');
$this->upload->initialize($config); //$config is array like as [in CI's documentation][1]
$path='uploads/some_folder'; // refers to the root's uploads/some_folder folder
$upload_data = $this->upload->multiFileUpload($path, TRUE);
神奇之处在于multiFileUpload()
我从别人那里得到的功能。通过将 CI 的 Upload 库放在我的 /application/libraries 文件夹中的一个名为 MY_Upload.php 的文件中,此函数在 CI 的 Upload 库上进行了扩展。
这是 MY_Upload.php 的全部内容,它实际上只是一个函数,所以不要被吓倒,除了你想要文件去哪里multiFileUpload()
之外,你不需要知道任何东西。$path
<?
class MY_Upload extends CI_Upload {
public function multiFileUpload($path, $protect = FALSE){
/*
* Declare uploaded_info and uploaded_files
* when i'm sure $_FILES has some data
*/
/* if($this->upload_path[strlen($this->upload_path)-1] != '/')
$this->upload_path .= '/';*/
//$this->upload_path=$path;
if(isset($_FILES)){
#Here we check if the path exists if not then create
/*if(!file_exists($this->upload_path)){
@mkdir($this->upload_path,0700,TRUE);
}*/
if(!file_exists($path)){
@mkdir($path,0700,TRUE);
}
$uploaded_info = FALSE;
/*
* The structure of $_FILES changes a lot with the array name on the input file,
* then i'm gonna modify $_FILES to make it think the data comes from several
* input file instead of one "arrayfied" input.
*
* The several ways to upload files are controled with this if...else structure
*/
if(count($_FILES) == 1)
{
$main_key = key($_FILES);
if(is_array($_FILES[$main_key]['name']))
{
foreach($_FILES[$main_key] as $key => $value)
{
for($y = 0; $y < count($value); $y++)
{
$_FILES[$main_key .'-'. $y][$key] = $value[$y];
}
}
unset($_FILES[$main_key]);
$uploaded_files = $_FILES;
}
else
{
$uploaded_files = $_FILES;
}
}
else
{
$uploaded_files = $_FILES;
}
#Here we create the index file in each path's directory
/*if($protect){
$folder = '';
foreach(explode('/',$this->upload_path) as $f){
$folder .= $f.'/';
$text = "<?php echo 'Directory access is forbidden.'; ?>";
if(!file_exists($folder.'index.php')){
$index = $folder.'index.php';
$Handle = fopen($index, 'w');
fwrite($Handle, trim($text));
fclose($Handle);
}
}
}*/
#Here we do the upload process
foreach($uploaded_files as $file => $value){
if (!$this->do_upload($file))
{
$uploaded_info['error'][] = array_merge($this->data(),
array('error_msg' => $this->display_errors()));
}
else
{
$uploaded_info['upload_data'][] = array_merge($this->data(),
array('error_msg' => $this->display_errors()));
}
}
}
#Then return what happened with the files
return $uploaded_info;
}
}