Reputation: 15
I'm trying to pass arrays from a form to php, and everything works fine until in php file it reaches Foreach loop, inside it it runs only one time (on all other loop cycles var_dump gives NULL).
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 array content:
["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)
}
Upvotes: 0
Views: 269
Reputation: 20430
(Posted on behalf of the OP).
Probably $_FILES array was overwrited, because it was mentioned two times. After changing name it started to work, plus there was some logical errors.
Upvotes: 0
Reputation: 4292
Look at the contents of $_FILES
. My guess is you'll see only my_file_upload0
, where as in your loop you're trying to loop through my_file_upload0
, my_file_upload1
, my_file_upload2
etc.
You need to loop through $_FILES['my_file_upload0']
itself, as this should be an array containing all of the files selected in the file input on your form.
foreach($_FILES['my_file_upload0'] as $key => $files) {
// Code
}
Edit - I'd actually recommend changing the name of my_file_upload0
to just my_file_upload
- for clarity.
Second Edit -I've replicated what you're doing with the supplied array here - http://phpfiddle.org/main/code/j88g-i2ci This goes through the for
loop twice without issue.
Upvotes: 1