How I can allow user to upload only IMAGE file ?
function.php
// Insert Attachment
function insert_attachment($file_handler, $post_id, $setthumb='false') {
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __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');
//echo $attach_id = media_handle_upload($file_handler, $post_id);
$attach_id = media_handle_upload($file_handler, $post_id);
if ($setthumb == 1) update_post_meta($post_id, '_thumbnail_id', $attach_id);
return $attach_id;
}
template-post.php
//Featured Image
if ($_FILES) {
array_reverse($_FILES);
$i = 0; //this will count the posts
foreach ($_FILES as $file => $array) {
if ($i == 0) $set_feature = 1; //if $i ==0 then we are dealing with the first post
else $set_feature = 0; //if $i!=0 we are not dealing with the first post
$newupload = insert_attachment($file, $post_id, $set_feature);
//echo $i++;
}
}
Need your help to complete this.
How I can allow user to upload only IMAGE file ?
function.php
// Insert Attachment
function insert_attachment($file_handler, $post_id, $setthumb='false') {
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __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');
//echo $attach_id = media_handle_upload($file_handler, $post_id);
$attach_id = media_handle_upload($file_handler, $post_id);
if ($setthumb == 1) update_post_meta($post_id, '_thumbnail_id', $attach_id);
return $attach_id;
}
template-post.php
//Featured Image
if ($_FILES) {
array_reverse($_FILES);
$i = 0; //this will count the posts
foreach ($_FILES as $file => $array) {
if ($i == 0) $set_feature = 1; //if $i ==0 then we are dealing with the first post
else $set_feature = 0; //if $i!=0 we are not dealing with the first post
$newupload = insert_attachment($file, $post_id, $set_feature);
//echo $i++;
}
}
Need your help to complete this.
Share Improve this question asked Dec 25, 2019 at 22:11 memsmems 7312 bronze badges1 Answer
Reset to default 4You can check mime type of uploaded image before upload to media. Add mimeTypes in $allowmimeType
which you want to allow. then check uploaded files mimetype $fileMimeType
. If not found in allowed mimetype then return false.
// Insert Attachment
function insert_attachment($file_handler, $post_id, $setthumb='false') {
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK){ return __return_false();
}
# uploaded file type
$fileMimeType = $_FILES[$file_handler]['type'];
# allowed types
$allowmimeType = array(
'png' => 'image/png',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
);
# check if mime type type
if(!in_array($fileMimeType,$allowmimeType) ){
return __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');
//echo $attach_id = media_handle_upload($file_handler, $post_id);
$attach_id = media_handle_upload($file_handler, $post_id);
if ($setthumb == 1) update_post_meta($post_id, '_thumbnail_id', $attach_id);
return $attach_id;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744879534a4598753.html
评论列表(0条)