フォームからPHPへ配列を渡そうとしていて、PHPのファイルでForeachループに到達するまでうまくいきます。 var_dumpはNULLを返します)。HTMLフォームからPHPとWordPressへの配列の受け渡し
HTML:
<div id="dynamicInput">
<div class="project">
<span>Project images</span> <input id="pr_img" multiple type='file' name='my_file_upload0[]'></br>
<input id="pr_name" type='text' placeholder='Project name' name='pname[]'>
<textarea id="pr_desc" cols="40" rows="8" placeholder='Project description' name='pdescr[]'></textarea>
</div>
<script>
var counter = 1;
var limit = 99;
function addInput(divName){
if (counter == limit) {
alert("You have reached the limit of adding " + counter + " inputs");
}
else {
var newdiv = document.createElement('div');
newdiv.className = "project";
newdiv.innerHTML = " <span>Project images</span> <input multiple id='pr_img' type='file' name='my_file_upload" + counter + "[]'></br><input id='pr_name' type='text' placeholder='Project name' name='pname[]'> <textarea cols='40' rows='8' type='text' id='pr_desc' placeholder='Project description' name='pdescr[]'></textarea>";
document.getElementById(divName).appendChild(newdiv);
counter++;
}
}
</script>
</div>
PHP:
require_once(ABSPATH . 'wp-admin/includes/image.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
$combo = array_combine($_POST[pname], $_POST[pdescr]);
$countt=0;
foreach ($combo as $nam => $descri) {
$projectid =wp_insert_post(array(
'post_title' => $nam,
'post_type' => 'Projects',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => $descri,
'post_status' => 'publish',
'post_author' => $userrid,
'menu_order' => 0,
));
/*---------------------This is the place where it doesnt work------------*/
$files = $_FILES["my_file_upload".$countt.""];
++$countt;
/*-----------------------------------------------------------------------*/
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("my_image_upload" => $file);
//var_dump($_FILES);
foreach ($_FILES as $file => $array) {
// $newupload = my_handle_attachment($file,$post_id);
$attach_id = media_handle_upload($file, 0);
}
}
unset($files);
}
add_post_meta($projectid, 'project_pics', $attach_id, true);
if (is_wp_error($attachment_id)) {
echo "Error while uploading images";
} else {
}
}
EDIT:$ _FILES配列の内容:$_FILES
の内容で
["my_file_upload0"]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(6) "bg.bmp"
[1]=>
string(6) "bg.png"
}
["type"]=>
array(2) {
[0]=>
string(9) "image/bmp"
[1]=>
string(9) "image/png"
}
["tmp_name"]=>
array(2) {
[0]=>
string(38) "/customers/c/e/b/********/phpO2lni7"
[1]=>
string(38) "/customers/c/e/b/********/phpcIObqF"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(64)
[1]=>
int(10946)
}
}
["my_file_upload1"]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(12) "job-icon.png"
[1]=>
string(8) "nova.png"
}
["type"]=>
array(2) {
[0]=>
string(9) "image/png"
[1]=>
string(9) "image/png"
}
["tmp_name"]=>
array(2) {
[0]=>
string(38) "/customers/c/e/b/********/phpZKskyd"
[1]=>
string(38) "/customers/c/e/b/********/phpbMlgIL"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(15213)
[1]=>
int(10906)
}
[回答の受け取りはどのように受け付けますか?](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work)をお読みください。解決策を質問ではなく答えに入れる。 – Quentin