plugins - Upload files programmatically to users

i'm using pods plugin to add new custom fields to user so i added 4 files custom fields and then trying to upload f

i'm using pods plugin to add new custom fields to user so i added 4 files custom fields and then trying to upload files to this fields from registration page and create new user but when i upload the files it did not generate all the images sizes and when open the new user from the admin panel the image not showing but the name is and the image post is created please any help

my code

 $uploadfiles = upload_files();

                $metas = array(
                    'uploadfiles1' => $uploadfiles[0]['attach_id'],
                    'uploadfiles2' => $uploadfiles[1]['attach_id'],
                    'uploadfiles3' => $uploadfiles[2]['attach_id'],
                    'uploadfiles4' => $uploadfiles[3]['attach_id'],

                );

                foreach ($metas as $key => $value) {
                    update_user_meta($user_id, $key, $value);
                }

upload_files function:

    function upload_files()
{
    $files_uploaded = array();
    $files = $_FILES;
    $files_extensions = ["image/png", "image/jpeg", "image/bmp", "image/jpg", "application/pdf"];

    $is_files_checked = check_upload_files($files, $files_extensions, 5);

    if ($is_files_checked) {

        foreach ($files as $key => $file) {

            $file_name = get_file_name($key);
            $upload_file_name = ($_POST['registration_fname'] . '_' . (!empty($_POST['registration_mname']) ? $_POST['registration_mname'] . '_' : '') . $_POST['registration_lname'] . '_' . $file_name . '_' . time() . get_file_extension($file['type']));
            $upload_dir = wp_upload_dir();

            if (move_uploaded_file($file["tmp_name"], $upload_dir['path'] . "/" . $upload_file_name)) {
                //registration_FrontDriverLicense || registration_BackDriverLicense
                //registration_FrontEmirateID || registration_BackEmirateID
                //Passport || Visa_Stamp
                $uploaded_file['file_name'] = $file_name;
                $uploaded_file['upload_url'] = $upload_dir['url'] . "/" . $upload_file_name;

                $attachment = array(
                    'guid'           => $uploaded_file['upload_url'],
                    'post_mime_type' => $file['type'],
                    'post_title'     => $upload_file_name,
                    'post_content'   => '',
                    'post_status'    => 'inherit'
                );

                $uploaded_file['attach_id'] = wp_insert_attachment( $attachment , $upload_dir['path']);
                require_once( ABSPATH . 'wp-admin/includes/image.php' );

                 //Generate the metadata for the attachment, and update the database record.
                $attach_data = wp_generate_attachment_metadata( $uploaded_file['attach_id'] , $upload_dir['path']);
                wp_update_attachment_metadata( $uploaded_file['attach_id'], $attach_data);

                $files_uploaded[] = $uploaded_file;
            }
        }
        return $files_uploaded;
    } else {
        //$is_files_checked files error....
        return $is_files_checked;
    }
}

i'm using pods plugin to add new custom fields to user so i added 4 files custom fields and then trying to upload files to this fields from registration page and create new user but when i upload the files it did not generate all the images sizes and when open the new user from the admin panel the image not showing but the name is and the image post is created please any help

my code

 $uploadfiles = upload_files();

                $metas = array(
                    'uploadfiles1' => $uploadfiles[0]['attach_id'],
                    'uploadfiles2' => $uploadfiles[1]['attach_id'],
                    'uploadfiles3' => $uploadfiles[2]['attach_id'],
                    'uploadfiles4' => $uploadfiles[3]['attach_id'],

                );

                foreach ($metas as $key => $value) {
                    update_user_meta($user_id, $key, $value);
                }

upload_files function:

    function upload_files()
{
    $files_uploaded = array();
    $files = $_FILES;
    $files_extensions = ["image/png", "image/jpeg", "image/bmp", "image/jpg", "application/pdf"];

    $is_files_checked = check_upload_files($files, $files_extensions, 5);

    if ($is_files_checked) {

        foreach ($files as $key => $file) {

            $file_name = get_file_name($key);
            $upload_file_name = ($_POST['registration_fname'] . '_' . (!empty($_POST['registration_mname']) ? $_POST['registration_mname'] . '_' : '') . $_POST['registration_lname'] . '_' . $file_name . '_' . time() . get_file_extension($file['type']));
            $upload_dir = wp_upload_dir();

            if (move_uploaded_file($file["tmp_name"], $upload_dir['path'] . "/" . $upload_file_name)) {
                //registration_FrontDriverLicense || registration_BackDriverLicense
                //registration_FrontEmirateID || registration_BackEmirateID
                //Passport || Visa_Stamp
                $uploaded_file['file_name'] = $file_name;
                $uploaded_file['upload_url'] = $upload_dir['url'] . "/" . $upload_file_name;

                $attachment = array(
                    'guid'           => $uploaded_file['upload_url'],
                    'post_mime_type' => $file['type'],
                    'post_title'     => $upload_file_name,
                    'post_content'   => '',
                    'post_status'    => 'inherit'
                );

                $uploaded_file['attach_id'] = wp_insert_attachment( $attachment , $upload_dir['path']);
                require_once( ABSPATH . 'wp-admin/includes/image.php' );

                 //Generate the metadata for the attachment, and update the database record.
                $attach_data = wp_generate_attachment_metadata( $uploaded_file['attach_id'] , $upload_dir['path']);
                wp_update_attachment_metadata( $uploaded_file['attach_id'], $attach_data);

                $files_uploaded[] = $uploaded_file;
            }
        }
        return $files_uploaded;
    } else {
        //$is_files_checked files error....
        return $is_files_checked;
    }
}
Share Improve this question edited Feb 9, 2016 at 11:02 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Feb 9, 2016 at 7:52 FadiFadi 1712 silver badges6 bronze badges 1
  • Note that passing an array or object into update_user_meta will mean it gets PHP serialised. As a result when you use get_user_meta it'll get deserialized leaving you open to object deserialisation attacks. Instead, either store the same post meta key multiple times by ysing add_user_meta, or use json_encode and json_decode when saving/getting – Tom J Nowell Commented Sep 2, 2019 at 15:04
Add a comment  | 

1 Answer 1

Reset to default 2

it was just missing $upload_dir['path'] . "/" . $upload_file_name in

            $uploaded_file['attach_id'] = wp_insert_attachment($attachment, $upload_dir['path'] . "/" . $upload_file_name);
            require_once(ABSPATH . 'wp-admin/includes/image.php');

            //Generate the metadata for the attachment, and update the database record.
            $attach_data = wp_generate_attachment_metadata($uploaded_file['attach_id'], $upload_dir['path'] . "/" . $upload_file_name);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742361162a4429420.html

相关推荐

  • plugins - Upload files programmatically to users

    i'm using pods plugin to add new custom fields to user so i added 4 files custom fields and then trying to upload f

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信