有多个上传图片的前端帖子?

时间:2012-03-11 作者:Ehab Fawzy

我正在使用带有上传图像的前端帖子,

我的代码

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);
        }
上传一张这样的图片效果很好

 <input type="file" id="image" name="image">
但是当我将html更改为

 <input type="file" id="image" name="image[]" multiple="multiple" >
它停止上传

谁能帮帮我吗????

1 个回复
SO网友:Bainternet

这个multiple="multiple"multiple="" 输入文件标记的属性是相当新的,在跨浏览器中不受广泛支持,但如果您想这样做,

尝试以下操作:

<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>
现在,上载文件时,您需要对阵列使用修复程序才能使用该功能,因此请从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;
}
一旦你有了这个功能,你就可以这样做:

if ($_FILES) {
    fix_file_array($_FILES[$name]);
    foreach ($_FILES[$name] as $file => $fileitem){
        $newupload = insert_attachment($fileitem,$real_post_id);
    }
}
哪个应该有效

结束

相关推荐

404从wp-Content/Uploads/获取图像时

我在获取图像时获得404状态,http仍然包含该图像。图像显示在浏览器中,但404代码中断了一些应用程序。对wp内容/上载/的调用被重定向到。htaccess:<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\\.php$ - [L] RewriteRule (.*) /index.php?getfile=$1 [L] </IfModule>&