多次上传未按预期工作
我有一个表单,允许用户添加任意数量的上传表单。所以一开始就有一个,您可以单击“添加更多”来...添加更多。有道理。每个输入如下所示:
<input type="file" name="userfile[]" id="userfile[]">
但是,当您上传多个文件时,只会上传第一个文件。下面是我的PHP。我正在使用 CodeIgniter 和文件上传库。
$dir = random_string('alnum', 10);
mkdir('/Applications/MAMP/htdocs/extras/uploads/temp/'.$dir);
$this->load->library('upload');
$error = 0;
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];
$config['upload_path'] = '/Applications/MAMP/htdocs/extras/uploads/temp/'.$dir;
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$this->upload->initialize($config);
if ($this->upload->do_upload('userfile')):
$error += 0;
else:
$error += 1;
endif;
endfor;
if ($error > 0):
$error = array('error' => $this->upload->display_errors());
print_r($error);
else:
$data = array('upload_data' => $this->upload->data());
print_r($data);
endif;
如果我在 for 循环之前添加 echo count($_FILES['userfile']['name'];
,它会正确显示文件数。如果我在 for 循环之后使用它,它错误显示 1.
知道我在这里做错了什么吗?
I have a form that allows the user to add as many upload forms as they want. So initially there's one, and you can click "Add More" to... add more. Makes sense. Each input looks like this:
<input type="file" name="userfile[]" id="userfile[]">
But when you upload more than one file, only the first file gets uploaded. Below is my PHP. I'm using CodeIgniter and the File Upload library.
$dir = random_string('alnum', 10);
mkdir('/Applications/MAMP/htdocs/extras/uploads/temp/'.$dir);
$this->load->library('upload');
$error = 0;
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];
$config['upload_path'] = '/Applications/MAMP/htdocs/extras/uploads/temp/'.$dir;
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$this->upload->initialize($config);
if ($this->upload->do_upload('userfile')):
$error += 0;
else:
$error += 1;
endif;
endfor;
if ($error > 0):
$error = array('error' => $this->upload->display_errors());
print_r($error);
else:
$data = array('upload_data' => $this->upload->data());
print_r($data);
endif;
If I add an echo count($_FILES['userfile']['name'];
before the for loop, it correctly displays the number of files. If I use it after the for loop, it incorrectly displays 1.
Any idea what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最好的猜测是,一旦进入循环,您将使用以下几行覆盖值:
尝试将其更改为如下所示:
然后使用
if ($this->upload->do_upload('userfile_tmp')) :
My best guess is that once you enter the loop you overwrite the values using these lines:
Try changing that to something like this:
And then using
if ($this->upload->do_upload('userfile_tmp')):
几个月前我遇到了这个问题,并扩展了核心库以允许单个文件或文件数组(正如您所希望的那样)并适当地显示错误等。
它似乎适合我使用它的用途,不能保证 100% 但看看它是否有帮助,我想我评论了我所做的更改的不同部分
: com/tgriesser/Codeigniter-Libraries/blob/master/MY_Upload.php" rel="nofollow">https://github.com/tgriesser/Codeigniter-Libraries/blob/master/MY_Upload.php
I encountered this problem a few months back and extended the core library to either allow a single file or also a file array (as you're looking to do) and display errors, etc. appropriately.
It seemed to work for what I was using it for, can't guarantee it 100% but see if it helps at all, I think I commented different sections with changes I had made:
https://github.com/tgriesser/Codeigniter-Libraries/blob/master/MY_Upload.php
问题是这些行:
您正在用另一个值(字符串)覆盖变量(数组)。
The problem are these lines:
You are overwriting your variable, an array, with another value, a string.