我要我的答案作为一个更全面的解释添加@ SachinMarwa自己的答案。我提交的代码并不是不同的代码,而是添加了他的答案中未提及的一些行和特定内容。
即使看起来他的回答在技术上是正确的,也许他的回答也是正确的,但他们并不适合我。我必须研究这个问题以找出真正发生的事情,并且我了解了如何编写我自己的解决方案的过程中了解了足够多。
首先,请参阅Nana Partykar的评论,“在您的控制器中,我无法看到任何is_uploaded_file()函数?”该评论告诉我们,人们误解了两个名称相似但文件不同的文件。我知道,因为有一段时间我以为他们一定是指同一个文件,控制器文件(名为“Uploader.php”)。我几乎可以看到所有这些问题都提到了相同的“如何使用Ajax上传多个文件”教程,其中包括我自己的版本。我们都在使用的代码是完全一样的。
但是,控制器文件是“Uploader.php”。你看到$ this-> upload-> do_upload()或$ this-> upload-> do_upload('userfile')甚至$ this-> upload-> do_upload('files'),这是指系统/库模块文件名为“Upload.php”。请注意,在调用do_upload()函数之前,您必须调用以下行:$ this-> load-> library('upload',$ config);
Sachin Marwha给了我们一个循环遍历$ _FILES ['userfile']数组的循环。假设你上传了三张照片。每个$ _FILES ['userfile']元素本身由5个“属性”组成:name,type,tmp_name,error,size。您可以在PHP上看到这些$ _FILE属性。
你只是想传递一个文件在一次do_upload()。您不希望一次将所有三个(甚至20个)文件传递给do_upload。这意味着在调用do_upload()之前,必须将$ _FILES ['userfile']数组分解为单个文件。为此,我创建$ _FILES数组的$ _FILES ['f']元素。我通过在do_upload($ file ='userfile')函数中的system/library/Upload.php文件中设置断点来了解这一点,以了解我在哪里得到臭名昭着的“没有选择要上传的文件” (包括我自己)一直在抱怨。你会发现,这个函数使用你的表单发送给你的控制器的原始$ _FILES数组。但它实际上只使用表单中输入类型=文件的名称。如果您不告诉它表单输入的名称,它将默认为$ _FILES ['userfile']。事实证明,这是我最大的问题,因为如果我使用输入字段的名称,那么该字段会传递一个数组或一组文件,而不仅仅是一个文件。所以我不得不做一个特殊的$ _FILES ['f]元素,并且只传递$ _FILES ['f']。
这里是我做的方式,相信我,我尝试了所有版本的这个页面和其他人,而不仅仅是一个StackOverflow的,但其他的教程上,还有:
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i < $cpt; $i++)
{
unset($config);
$config = array();
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = FALSE;
$config['file_name'] = $_FILES['userfile']['name'][$i];
// Create a new 'f' element of the $_FILES object, and assign the name, type, tmp_name, error, and size properties to the corresponding 'userfile' of this iteration of the FOR loop.
$_FILES['f']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['f']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['f']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['f']['size'] = $_FILES['userfile']['size'][$i];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (! $this->upload->do_upload('f'))
{
$data['errors'] = $this->upload->display_errors();
}
else
{
$data['errors'] = "SUCCESS";
}
unset($config);
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = $path . $_FILES['userfile']['name'][$i];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['thumb_marker'] = '.thumb';
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
$types = array('.jpg');
}
凡会取消for循环中的$ config数组,然后重新映射$ config数组,这就是为每个图片文件制作缩略图的部分。
完整控制器上载功能:
public function upload_asset_photo()
{
$data = array();
$dateArray = explode("/",$this->input->post('date'));
$date = $dateArray[2] . "/" . $dateArray[0] . "/" . $dateArray[1]; // year/month/day
$cid = $this->config->item('cid'); // this is a special company id I use, unnecessary to you guys.
$padded_as_id = sprintf("%010d", $this->uri->segment(3)); // this makes an "asset id" like "3" into "0000000003"
$path = 'properties_/' . $padded_as_id . '/' . $date . '/'; // file path
if (!is_dir($path)) {
mkdir($path,0755,true); //makes the ile path, if it doesn't exist
}
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i < $cpt; $i++)
{
unset($config);
$config = array();
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = FALSE;
$config['file_name'] = $_FILES['userfile']['name'][$i];
$_FILES['f']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['f']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['f']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['f']['size'] = $_FILES['userfile']['size'][$i];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (! $this->upload->do_upload('f'))
{
$data['errors'] = $this->upload->display_errors();
}
else
{
$data['errors'] = "SUCCESS";
}
unset($config);
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = $path . $_FILES['userfile']['name'][$i];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['thumb_marker'] = '.thumb';
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
$types = array('.jpg');
}
header('Content-Type: application/json');
echo json_encode($data);
}
在你** **控制器,我不能看到任何'is_uploaded_file()'函数? –
不,我没有使用过这个功能..... –
do_upload();是用 –