如何在我的自定义页面中不使用任何插件上传多个文件?
答案 0 :(得分:0)
试试这个:
if (isset($_FILES['upload_image']))
{
foreach($_FILES['upload_image']['tmp_name'] as $key => $tmp_name)
{
$file_name = $key.$_FILES['upload_image']['name'][$key];
$file_size =$_FILES['upload_image']['size'][$key];
$file_tmp =$_FILES['upload_image']['tmp_name'][$key];
$file_type=$_FILES['upload_image']['type'][$key];
$uploadedfile = $file_name ;
$upload_name = $file_name;
$uploads = wp_upload_dir();
$filepath = $uploads['path'] . ” / $upload_name”;
if (!function_exists('wp_handle_upload'))
{
require_once (ABSPATH . 'wp-admin/includes/file.php');
}
require_once (ABSPATH . 'wp-admin/includes/image.php');
$upload_overrides = array(
'test_form' => false
);
$movefile = wp_handle_upload($uploadedfile, $upload_overrides);
if ($movefile && !isset($movefile['error']))
{
$file = $movefile['file'];
$url = $movefile['url'];
$type = $movefile['type'];
// media_handle_upload( $file_handler, 0 );
$attachment = array(
'post_mime_type' => $type,
'post_title' => $upload_name,
'post_content' => 'Image for ' . $upload_name,
'post_status' => 'inherit',
'post_parent' => $post->ID
//'post_parent' => 0 //IF DONT WANT TO ATTACH POST ID
);
$attach_id = wp_insert_attachment($attachment, $file, $post->ID);
$attach_id = wp_insert_attachment($attachment, $file, 0);//IF DONT WANT TO ATTACH POST ID
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
wp_update_attachment_metadata($attach_id, $attach_data);
echo $postid = wp_insert_post( $my_post );
}
}
}
不要忘记在表单标记
中添加enctype="multipart/form-data"