Ask your WordPress questions! Pay money and get answers fast! (more info)

upload multiple images from front-end issue WordPress

  • SOLVED

hi i am using this too codes for upload multiple images

if ( $_FILES ) {
$files = $_FILES['agp_gallery'];
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("agp_gallery" => $file);

foreach ($_FILES as $file => $array)
{
$newupload = agp_process_wooimage($file,$post_id);
}


}
}


and

function agp_process_wooimage($file, $post_id){

if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) __return_false();


require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');

$attachment_id = media_handle_upload($file, $post_id);
$post_id = array ();
$array = array_push($post_id, '_product_image_gallery', $attachment_id);

update_post_meta($post_id, '_product_image_gallery', $array);

return $attachment_id;

}


but i have little problem images are uploading fine and attach too but in wp_postmeta there is no meta_key for _product_image_gallery for images attachment id... please check my attachment image.

i don't understand why the "_product_image_gallery" is not creating ...


if i change

update_post_meta($post_id, '_product_image_gallery', $array);

to

add_post_meta($post_id, '_product_image_gallery', $array);

its create "_product_image_gallery" meta_key but multiple times for its different meta_value

example :-

meta_key meta_value
_product_image_gallery 1
_product_image_gallery 2
_product_image_gallery 3

Instead

meta_key meta_value
_product_image_gallery 1,2,3

thanks in advance for your help

Answers (2)

2013-06-28

Hariprasad Vijayan answers:

Hello,

Please try this
<?php
if ( $_FILES )
{
$files = $_FILES['agp_gallery'];
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("agp_gallery" => $file);
$i=1;
foreach ($_FILES as $file => $array)
{
$newupload = agp_process_wooimage($file,$post_id,$i);
$i++;
}
}
}
function agp_process_wooimage($file, $post_id, $count)
{
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attachment_id = media_handle_upload($file, $post_id);
$post_id = array ();
$array = array_push($post_id, '_product_image_gallery', $attachment_id);
update_post_meta($post_id, '_product_image_gallery'.$count, $attachment_id);
return $attachment_id;
}
?>


Hariprasad Vijayan comments:

Please ask if you want any help regarding this..


somendra meena comments:

hi thanks for your reply please check attach file now post_id showing wrong and only one meta_value i uploaded to images and show only one meta value


somendra meena comments:

this is result