Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using front end post with upload image,

my code

if (!function_exists('wp_generate_attachment_metadata')){
            require_once(ABSPATH . "wp-admin" . '/includes/image.php');
            require_once(ABSPATH . "wp-admin" . '/includes/file.php');
            require_once(ABSPATH . "wp-admin" . '/includes/media.php');
        }
         if ($_FILES) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                    return "upload error : " . $_FILES[$file]['error'];
                }
                $attach_id = media_handle_upload( $file, $new_post );
            }   
        }
        if ($attach_id > 0){
            //and if you want to set that image as Post  then use:
            update_post_meta($new_post,'_thumbnail_id',$attach_id);
        }

and it's work good with uploading one image like this

 <input type="file" id="image" name="image">

but when I change my html to

 <input type="file" id="image" name="image[]" multiple="multiple" >

it stop uploading

Can any one help me please????

share|improve this question
    
what do you mean Stop Uploading? Do you see any error? –  Sisir Mar 11 '12 at 12:10
    
no, no error, but no upload also. –  Ehab Fawzy Mar 11 '12 at 12:56
    
Have you enabled WP_DEBUG and such? –  kaiser Mar 11 '12 at 14:37
add comment

1 Answer

the multiple="multiple" or multiple="" attribute of input file tag is fairly new and not widely supported cross browser but if that is the way you want to go,

try this:

<form  ...
   <input type="file" id="image" name="image[]" onchange="updateList();" multiple="multiple" >
   <ul id="file_list"></ul>
</form>
<script>
function updateList(){
    //get the input and UL list
    var input = document.getElementById('image');
    var list = document.getElementById('file_list');

    //empty list for now...
    while (list.hasChildNodes()) {
      list.removeChild(ul.firstChild);
    }

    //for every file...
    for (var x = 0; x < input.files.length; x++) {
      //add to list
      var li = document.createElement('li');
      li.innerHTML = 'File ' + (x + 1) + ':  ' + input.files[x].name;
      list.append(li);
    }
}
</script>

now when the files are uploaded you need to use a fix to the array in order to use that function, so use these function from Golden Apples

function fix_file_array(&$files) {
    $names = array(
        'name' => 1,
        'type' => 1,
        'tmp_name' => 1,
        'error' => 1,
        'size' => 1
    );
    foreach ($files as $key => $part) {
        // only deal with valid keys and multiple files
        $key = (string) $key;
        if (isset($names[$key]) && is_array($part)) {
            foreach ($part as $position => $value) {
                $files[$position][$key] = $value;
            }
            // remove old key reference
            unset($files[$key]);
        }
    }
}


function insert_attachment($file_handler,$post_id,$setthumb='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');
    $attach_id = media_handle_sideload( $file_handler, $post_id );
    if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
    return $attach_id;
}

and once you have this function then you can do this:

if ($_FILES) {
    fix_file_array($_FILES[$name]);
    foreach ($_FILES[$name] as $file => $fileitem){
        $newupload = insert_attachment($fileitem,$real_post_id);
    }
}

which should work

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.