最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

uploads - Error uploading image in WordPress

programmeradmin0浏览0评论

Hello i am trying to upload an image from a form in WordPress but i keep getting: Error loading File! without any clue as to what am doing wrong:

require_once($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php'); 

if($_FILES['trainee_image']['name']) {
    if(!$_FILES['trainee_image']['error']) {

        //validate the file
        $new_file_name = strtolower($_FILES['trainee_image']['tmp_name']);

          //can't be larger than 200 KB 
        if($_FILES['trainee_image']['size'] > (200000)) {
            //wp_die generates a visually appealing message element
            wp_die('Your file size is to large.');
        }
        else {
            //the file has passed the test
            //These files need to be included as dependencies when on the front    end.
            require_once( ABSPATH . 'wp-admin/includes/image.php' );
            require_once( ABSPATH . 'wp-admin/includes/file.php' );
            require_once( ABSPATH . 'wp-admin/includes/media.php' );

            // Let WordPress handle the upload.
            // Remember, 'upload' is the name of our file input in our form above.
            $file_id = media_handle_upload( 'trainee_image', 0 );

            if ( is_wp_error( $file_id ) ) {
                wp_die('Error loading file!');
            } 
        }
    }
    else {
        //set that to be the returned message
        wp_die('Error: '.$_FILES['trainee_image']['error']);
    }
}
发布评论

评论列表(0)

  1. 暂无评论