概要
4つのファイルアップロードフィールドを持つマルチパートフォームがあり、そのうちの3つが動的に追加されます(JavaScriptを使用)。
<input type="file" name="OneCertificate" />
<input type="file" id="MultipleCertificate[1] "name="MultipleCertificate[]" />
<input type="file" id="MultipleCertificate[2] "name="MultipleCertificate[]" />
// more if Add button is pressed
これがの出力ですvar_dump($_FILES)
:
["OneCertificate"]=> array(5) {
["name"]=> string(6) "DE.pdf"
["type"]=> string(15) "application/pdf"
["tmp_name"]=> string(24) "C:\xampp\tmp\php37C2.tmp"
["error"]=> int(0)
["size"]=> int(103845)
}
// **Notice the attributes are all in their own arrays**
["MultipleCertificate"]=> array(5) {
["name"]=> array(2) { [0]=> string(6) "DE.pdf" [1]=> string(6) "DE.pdf" }
["type"]=> array(2) { [0]=> string(15) "application/pdf" [1]=> string(15) "application/pdf" }
["tmp_name"]=> array(2) { [0]=> string(24) "C:\xampp\tmp\phpD941.tmp" [1]=> string(24) "C:\xampp\tmp\phpD942.tmp" }
["error"]=> array(2) { [0]=> int(0) [1]=> int(0) }
["size"]=> array(2) { [0]=> int(103845) [1]=> int(103845) }
}
// and so on...
以下は、各ファイルをアップロードする方法です。
function upload_file($field_name)
{
// timestamp name: http://stackoverflow.com/questions/7457152/did-not-select-a-file-to-upload-when-uploading-using-codeigniter
$the_date= date('Y/n/j h:i:s');
$replace = array(":"," ","/");
$new_name = str_ireplace($replace, "-", $the_date);
$config['upload_path'] = './uploads/';
$config['file_name'] = $new_name;
$config['allowed_types'] = 'pdf|jpg|jpeg|png';
$this->load->library('upload');
$this->upload->initialize($config);
// Get OneCertificate the normal way since it will only have one file content
if ( $field_name == 'OneCertificate' ) {
if ( ! $this->upload->do_upload($field_name)) {
return array('error' => $this->upload->display_errors());
} else {
$file_data = array('upload_data' => $this->upload->data());
}
// Method for MultipleCertificate
} else {
for ($i = 0; $i < count($_FILES[$field_name]['name']); $i++) {
if ( ! $this->upload->do_upload($_FILES[$field_name]['name'][$i])) {
$file_data = array('error' => $this->upload->display_errors());
} else {
$file_data = array('upload_data' => $this->upload->data());
}
} // END for loop
}
return $file_data;
}
問題
それぞれの属性が独自の配列にあるのに比べて、すべての情報が単一の配列にあるOneCertificate
ため、作品の形式に気づきました。MultipleCertificate
前者はファイルを正常にアップロードできますが、後者はアップロードするファイルを選択していません。
MultipleCertificate
のフォームをに変換および/または取得するにはどうすればよいOneCertificate
ですか?
注:これは、データベース挿入用に作成さ$OneCertificate
れた配列を割り当てるために必要なフォームです。$MultipleCertificate