我有一個表單,允許用戶添加任意數量的上傳表單。所以最初有一個,你可以點擊「添加更多」以添加更多內容。說得通。每個輸入如下:多個上傳按預期工作
<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.
任何想法我在做什麼錯在這裏?
謝謝,這工作。 – dallen 2011-05-19 05:44:48