简体   繁体   中英

Uploading images from Wordpress frontend

I am attempting to create a frontend form for users to upload multiple images and attach them to a post.

The post is posting just fine, all the data is attached. But I keep getting these two errors:

Warning: Illegal offset type in mysite/wp-admin/includes/media.php on line 254

Warning: Illegal offset type in mysite/wp-admin/includes/media.php on line 255

I've been trying this for a long time now and have searched high and low. I cannot seem to get this working. I have simplified the form to only upload one image for now, which that isn't working either. Below is the PHP I am using to process the upload:

require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');


if ($_FILES) {
     $attach_id = media_handle_upload( $_FILES['upl'], $rid );
     set_post_thumbnail($rid, $attach_id);
     update_post_meta($rid,'_thumbnail_id',$attach_id);
}

Where $rid is the post ID.

The input field is <input type="file" name="upl" class="ride_image"/>

I have already checked my max upload size, and the $_FILES global is returning the image properly. Here is an example var_dump($_FILES);

array(1) { ["upl"]=> array(5) { ["name"]=> string(21) "rizkdbr2live_lead.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(14) "/tmp/phpgPwO8W" ["error"]=> int(0) ["size"]=> int(85529) } }

Keep in mind, this is the most simplified this form will be. I am just trying to get the image to actually upload right now. I will be adding in validation and things like that.

Any idea what those two Illegal offset type errors are and how I can fix them? This is becoming a very, very tedious task which should be pretty easy. Would it be against all standards to write custom PHP outside of wordpress to process this image upload or is using media_handle_upload() the only way to really do this? I want to abide by standards as much as possible.

From wordpress codex:

<?php media_handle_upload( $file_id, $post_id, $post_data, $overrides ); ?>

$file_id 
(string) (required) Index into the $_FILES array of the upload

http://codex.wordpress.org/Function_Reference/media_handle_upload

So you need to pass upl instead $_FILE['upl']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM