无法从Codeigniter中的1个表单保存2个文件 [英] not able to save 2 files from 1 form in codeigniter
问题描述
我有一个表格,要求用户上传2个文件,文件上传和 imgupload 。我正在尝试通过ajax保存整个表单。
I have a form that requires a user to upload 2 files, file-upload & imgupload. I am trying to save the entire form through ajax.
表单和Ajax代码
<form enctype="multipart/form-data" id="data">
<input type="file" id="file-upload" class="file-upload" name="file-upload" >
<input type="file" id="imgupload" class="imgupload" name="imgupload" >
<button type="submit" id="save" class="save-icon-btn">
<img src="<?php echo base_url(); ?>assets/img/Save.png">
</button>
</form>
$("#save").click(function()
{
var form_data = new FormData($('#data')[0]);
jQuery.ajax(
{
type: "POST",
url: "<?php echo base_url(); ?>" + "comp/wfc",
data: form_data,
processData: false,
contentType: false,
success: function(res)
{
console.log(res);
alert(res);
}
});
});
控制器代码
$config['upload_path'] = './assets/file/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('file-upload'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
else
{
$data = $this->upload->data();
echo $res = $data['file_name'];
}
$config2['upload_path'] = './assets/img/.';
$config2['allowed_types'] = 'gif|jpg|png|doc|docx|txt';
$config2['max_size'] = 1024 * 8;
$config2['encrypt_name'] = TRUE;
$this->load->library('upload', $config2);
if (!$this->upload->do_upload('imgupload'))
{
$error1 = array('error' => $this->upload->display_errors());
print_r($error1);
}
else
{
$data1 = $this->upload->data();
echo $res1 = $data1['file_name'];
}
问题在于,在控制器代码中,只有1个文件被上传。如果我将file-upload保留为第一个上传代码,那么只有该文件将被上传,而imgupload将不会上传。
The issus is that in the controller code only 1 of the files get uploaded. If i keep file-upload as the first upload code then only this will get uploaded and imgupload will not get uploaded.
如果我保留imgupload作为第一个上传代码,则只有这样才能上传,而不会上传文件。
And if i keep imgupload as the first upload code then only this will get uploaded and file-upload will not get uploaded.
我不明白为什么会这样。谁能告诉您解决该问题的方法吗?
I am not able to understand why this is happening. Can anyone please tell a solution to this problem
推荐答案
请尝试使用此更新的功能。您正在做一些奇怪的事情,例如初始化第一个而不是第二个。由于CI是单例,因此可能会导致某些问题(不是说这是根本原因)。
Please try this updated function. You were doing some strange things e.g. initializing the first and not the second. As CI is a singleton, this could result in some issues (not saying that this was the root cause).
$this->load->library('upload');
echo '<pre>';
print_r($_FILES);
$config['upload_path'] = './assets/file/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->upload->initialize($config);
if (!$this->upload->do_upload('file-upload')) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
} else {
$data = $this->upload->data();
echo $res = $data['file_name'];
}
$config2['upload_path'] = './assets/img/.';
$config2['allowed_types'] = 'gif|jpg|png|doc|docx|txt';
$config2['max_size'] = 1024 * 8;
$config2['encrypt_name'] = TRUE;
$this->upload->initialize($config2, true);
if (!$this->upload->do_upload('imgupload')) {
$error1 = array('error' => $this->upload->display_errors());
print_r($error1);
} else {
$data1 = $this->upload->data();
echo $res1 = $data1['file_name'];
}
如果不起作用。请让我们知道 $ _ FILES
数组正在打印什么。
If it doesn't work. Please let us know what the $_FILES
array is printing.
更新:
无法正常工作,因为 $ this-> load-> library('upload',$ config2);
。当CI看到加载时,它将首先检查是否加载了该类,而与传递的参数无关,因为已经加载了该行,因此忽略了该行,并且 $ config
中的设置(第一个图片上传)。
It wasn't working because $this->load->library('upload', $config2);
. When CI sees load it checks first if the class is loaded regardless of the parameters being passed, as it was already loaded this line was ignored and the settings from $config
(the first image upload) were being used.
这篇关于无法从Codeigniter中的1个表单保存2个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!