1
所有, 我使用這個輸入域的PHP文件時:非法偏移類型上傳
<input type="file" name="image_to_upload_image" accept="image/*">
然後我試圖處理與下面的代碼文件上傳:
if ($_FILES["image_to_upload_image"]) {
echo "it will try and upload";
$file = $_FILES["image_to_upload_image"];
print_r($file);
$newupload = bmt_handle_attachment_new_post($file,$post_id);
echo "The newupload is: ".$newupload;
set_post_thumbnail($post_id, $newupload);
}
它調用下面的函數:
function bmt_handle_attachment_new_post($file_handler,$post_id,$set_thu=false) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __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');
$attachment_id = media_handle_upload($file_handler, $post_id);
if (is_wp_error($attachment_id)) {
// There was an error uploading the image.
} else {
// The image was uploaded successfully!
}
// If you want to set a featured image from your uploads.
return $attachment_id;
}
我在做同樣的事情,這個例子: http://codex.wordpress.org/Function_Reference/media_handle_upload
爲什麼說非法偏移類型?
更新:我有一個具有相同的輸入字段,只不過它處理多文件上傳另一種形式:
<input type="file" name="kv_multiple_attachments[]" multiple="multiple" >
如果我當時把它傳遞給下面的表單處理程序:
if ($_FILES) {
$files = $_FILES["kv_multiple_attachments"];
$gallery_id = $_POST['gallery_for_upload'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("kv_multiple_attachments" => $file);
foreach ($_FILES as $file => $array) {
$newupload = bmt_handle_attachment($file,$pid,$gallery_id);
//$thumb_url = wp_get_attachment_thumb_url($newupload);
//echo "<img src=".$thumb_url.">";
}
}
}
}
然後使用相同的處理功能:
function bmt_handle_attachment($file_handler,$post_id,$gallery_id,$set_thu=false) {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __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');
$attachment_id = media_handle_upload($file_handler, $post_id);
update_post_meta($attachment_id, 'bmt_gallery_id', $gallery_id);
// If you want to set a featured image frmo your uploads.
return $attachment_id;
}
上傳的文件沒有問題。
什麼是'$ post_id'?你爲什麼將'$ _FILES [「image_to_upload_image」]'作爲'$ file_handler'傳遞,然後執行'$ _FILES [$ file_handler]'? – 2014-10-31 19:24:59
@ Fred-ii-這是一個複製粘貼錯誤。問題已更新。 – user1048676 2014-10-31 19:25:37
看的不錯時間;)*第一條評論刪除* – 2014-10-31 19:26:08