我正在尝试在文件夹中上传多个文件,但它会显示错误“您没有选择要上传的文件。”
遇到PHP错误
严重性:警告
消息:is_uploaded_file()期望参数1为字符串,数组 给定
文件名:libraries / Upload.php
行号:412
回溯:
文件:C:\ wamp \ www \ mshaadi \ application \ controllers \ Email.php行:55 功能:do_upload
文件:C:\ wamp \ www \ mshaadi \ index.php行:293功能:require_once
控制器
$conf['upload_path'] = './images';
$conf['allowed_types'] = 'doc|docx|pdf|jpg|gif|jpeg|png';
$conf['max_size'] = '9999000';
$conf['max_width'] = '1024';
$conf['max_height'] = '768';
$conf['overwrite'] = TRUE;
$this->load->library('upload');
foreach ($_FILES as $fieldname => $fileObject){
$this->upload->initialize($conf);
if (!empty($fileObject['name'])){
if (!$this->upload->do_upload($fieldname)){
$error = $this->upload->display_errors();
print_r($error);
}else{
print_r("done");
}
}else {
print_r("no");
}
}
查看
<div class="form-group col-md-12">
<label for="Attach"><strong>Add Attachment</strong><br></label>
<input type="file" class="btn btn-default btn-file" name="atta[]" id="Attach" multiple="multiple">
</div>
答案 0 :(得分:6)
试试这个,
function upload_files()
{
$config = array();
$config['upload_path'] = './Images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
$this->load->library('upload');
$files = $_FILES;
for($i=0; $i< count($_FILES['userfile']['name']); $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($config);
$this->upload->do_upload();
}
}
答案 1 :(得分:1)
这是有效的
function do_upload()
{
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
}
}
private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './Images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
return $config;
}
答案 2 :(得分:1)
我将把我的答案添加为@ SachinMarwa自己答案的更全面的解释。我提交的代码不是不同的代码,而是添加了他的答案中未提及的一些行和细节。
即使看起来他的答案在技术上是正确的,也许他上面的答案也以他们自己的方式正确,但他们并不适合我。我不得不研究这个问题来找出真正发生的事情,并且我对这个过程有了足够的了解,以了解如何编写我自己的解决方案。
首先,请参阅Nana Partykar的评论,“在你的控制器中,我无法看到任何is_uploaded_file()函数?”该评论告诉我们,人们误解了两个名称相似但文件不同的文件。我知道,因为有一段时间我认为他们必须引用同一个文件,控制器文件(名为“Uploader.php”)。我可以看到几乎所有这些问题都引用了相同的“如何使用Ajax上传多个文件”教程,包括我自己的版本。我们所使用的代码完全相同。
但是,控制器文件是“Uploader.php”。你看到$ this-&gt; upload-&gt; do_upload()或$ this-&gt; upload-&gt; do_upload('userfile')甚至$ this-&gt; upload-&gt; do_upload('files'),这个是指名为“Upload.php”的系统/库模块文件。请注意,在调用do_upload()函数之前,您必须调用以下行:$ this-&gt; load-&gt; library('upload',$ config);
Sachin Marwha给了我们一个循环遍历$ _FILES ['userfile']数组的for循环。假设您上传了三张图片。每个$ _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数组。但它实际上只使用表单中输入type = file的名称。如果您没有告诉它表单输入的名称,它将默认为$ _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 i循环中取消设置$ 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);
}
答案 3 :(得分:0)
这应该有效
public function add()
{
$config = array();
$config['upload_path'] = './assets/uploaded_images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$this->load->library('upload');
$image_names = array();
$files = $_FILES;
if (isset($_FILES['images'])) {
for ($i = 0; $i < count($_FILES['images']['name']); $i++) {
$_FILES['image']['name'] = $files['images']['name'][$i];
$_FILES['image']['type'] = $files['images']['type'][$i];
$_FILES['image']['tmp_name'] = $files['images']['tmp_name'][$i];
$_FILES['image']['error'] = $files['images']['error'][$i];
$_FILES['image']['size'] = $files['images']['size'][$i];
$this->upload->initialize($config);
$image_names[$i] = $_FILES['image']['name'];
$this->upload->do_upload('image');
}
$data1['images'] = implode(',', $image_names);
$data1['productId'] = $this->input->post('productId');
$image_id = $this->Image_model->add_image($data1);
redirect('image/index');
} else {
$error = array('error' => $this->upload->display_errors());
$this->load->model('Product_model');
$data['all_products'] = $this->Product_model->get_all_products();
$data['error'] = $error;
$data['_view'] = 'image/add';
$this->load->view('layouts/main', $data);}
}