1
我試圖創建8個字段上傳圖像,使用add_meta_box,但由於某種原因,不會上傳一些圖像(jpeg),無論其文件大小是否正確,寬度和高度也是正確的。在wordpress元框上的多個圖像上傳
字段:
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);
手之前我只是在學習,所以我對任何大錯(或恐怖)在道歉我的代碼。
所有這一切工作正常,直到我有一些圖像不會更新,這些圖像是JPEG格式,文件大小和尺寸是代碼中的值。
使用:
echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";
我發現的值是否正確(圖像通過「驗證」),但圖像沒有得到上傳。請注意,該腳本沒有上傳其他JPEG圖像的問題。
任何幫助將不勝感激提前
要命,感謝update_facilitiesimage功能!它的效果很好 – user1068410 2012-05-09 15:30:56