I have a custom form for editing custom post and this form is for frontend users to edit their posts and respective attachments. For the attachments part I have two input file elements in the form where an image and a video can be uploaded and I arrived at below PHP code to capture the uploaded attachments.
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK || $_FILES[$file]['error'] === UPLOAD_ERR_NO_FILE) {
$ermg = "upload error : " . $_FILES[$file]['error'];
continue;
} else {
$attach_id = media_handle_upload($file, $post_id);
$type = get_post_mime_type($attach_id);
if ($type === 'image/jpeg' || $type === 'image/png') {
update_post_meta($new_post, '_thumbnail_id', $attach_id);
} elseif ($type === 'video/mp4' || $type === 'video/quickime') {
update_post_meta($new_post, '_video_id', $attach_id);
}
}
return $ermg;
}
}
Here the problem is my code is not capturing only one attachment if both are uploaded at same time, where as uploading a single attachment works fine.
Along with this there is one more issue, I am seeing a new unknown attachment with no title being formed in the media library every time some upload happens, below is the screenshot and this new unknown attachment is not expected. Can anyone please help?