我正在尝试使用 add_meta_box 创建 8 个字段以在帖子上上传图像,但由于某种原因,某些图像 (jpeg) 将不会被上传,无论它们的文件大小是否正确以及宽度和高度是否正确。
田野:
function facilitiesimage_box_render(){
$status_message = get_post_meta($post->ID, 'facilities_img_error', true);
if($status_message) {
echo '<div class="error" id="message"><p>' . $status_message . '</p></div>';
}
$max_no_img=8; // Maximum number of images value to be set here
echo '<input type="hidden" name="MAX_FILE_SIZE" value="1524288">';
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Images $i</td><td><input type=file name='images[]' class='bginput'></td></tr>";
}
echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>";
echo "</form> </table>";
}
其余代码:
function update_facilitiesimage($post_id, $post) {
while(list($key,$value) = each($_FILES['images']['name'])) {
if(!empty($value)){
$arr_file_type = wp_check_filetype(basename($value));
$uploaded_file_type = $arr_file_type['type'];
$allowed_file_types = array('image/jpg', 'image/jpeg', 'image/gif', 'image/png');
if(in_array($uploaded_file_type, $allowed_file_types)){
$upload_dir = wp_upload_dir();
$newname = strtolower(str_replace(' ', '-', $value));
$add = $upload_dir['path'] . '/' . $newname;
copy($_FILES['images']['tmp_name'][$key], $add);
list($width, $height) = getimagesize($add);
$upload_image_width = '630';
$upload_image_height = '350';
if($width < $upload_image_width || $height < $upload_image_height) {
$upload_feedback = 'Sorry, the image ' . $value . ' does not meet the minimum height/width requirements. Please upload another image.<br/>';
update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
unlink($add);
}
else {
$upload_image_width = '630';
$upload_image_height = '350';
$resized = image_resize($add, $upload_image_width, $upload_image_height, true);
unlink($add);
$file_name_and_location = $add;
$file_title_for_media_library = 'your title here';
$attachment = array(
'post_mime_type' => $uploaded_file_type,
'post_title' => 'Uploaded image ' . addslashes($file_title_for_media_library),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file_name_and_location );
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file_name_and_location );
wp_update_attachment_metadata($attach_id, $attach_data);
$ra = 'attached_facility-img' . $key;
$existing_uploaded_image = (int) get_post_meta($post_id, $ra, true);
if(is_numeric($existing_uploaded_image)) {
wp_delete_attachment($existing_uploaded_image);
}
update_post_meta($post_id, $ra ,$attach_id);
$upload_feedback = false;
update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
$fname = strrchr($resized, '/');
$img = $upload_dir['baseurl'] . $fname;
$table = 'facilitiesimage-' . $key;
delete_post_meta($post_id, $table);
update_post_meta($post_id, $table, $img);
}
}
else {
$upload_feedback = 'Please upload only image files (jpg, gif or png).<br/>';
update_post_meta($post_id, 'facilities_img_error', $upload_feedback);
}
}
}
}
add_action('save_post','update_facilitiesimage',1,2);
在此之前我只是在学习,所以我为我的代码中的任何 HUGE ERROR(或恐怖)道歉。
一切正常,直到我得到一些不会更新的图像,这些图像是 jpeg,文件大小和尺寸与代码中的值有关。
使用:
echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";
我发现这些值是正确的(图像通过了“验证”),但图像没有上传。请注意,该脚本上传其他 jpeg 图像没有问题。
任何帮助将不胜感激
提前致谢