这个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 Applesfunction 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);
}
}
哪个应该有效