wp_insert_attachment 未将图像上传到上传文件夹

时间:2021-03-16 10:18:02

标签: wordpress email qr-code custom-wordpress-pages

我的网站中有一个功能可以生成二维码,然后将其上传到上传文件夹并通过电子邮件发送给用户。这在本地主机上完美运行,但图像没有上传到网络服务器上的上传文件夹。我将文件夹权限更改为 777(最大)但没有,我还联系了我的托管服务提供商,但他们说问题不在于服务器。当我在 wp-admin/media 上传图片时,图片上传到文件夹没有任何问题.. 可能是什么问题?

function poker_membership_card_generated($membership_plan, $args)
    {
        $user_membership_id = $args['user_membership_id'];
        $membership = wc_memberships_get_user_membership($user_membership_id);

        require_once 'vCard.php';
        $vcard = new vCard;
        $vcard->setName(poker_get_user_full_name(get_user_by('id', $args['user_id'])), '');
        $vcard->setTitle($membership_plan->name . " Card");

        $membership_end_date = '';
        if ($membership_plan->get_access_length_amount()) {
            if ($args['is_update']) {
                $now = get_post_meta($user_membership_id, '_end_date', true);
            } else {
                $now = current_time('mysql', true);
            }
            $membership_end_date = $membership_plan->get_expiration_date($now, $args);
            $membership_end_date = date('m-d-Y H:i:s', strtotime($membership_end_date));
        }

        $note = "Date end: " . $membership_end_date . "\nCard id: $membership->id";
        $vcard->setNote($note);

        //image qr code
        $attach_id = poker_generate_membership_qr_code($vcard, $args['user_id']);
        
        update_field('qrcode_image', $attach_id, $args['user_membership_id']);
  //$attachments = get_attached_file($attach_id);
        //sending email with card
        $user = get_user_by('id', $args['user_id']);
        $subject = "Membership Card Status Changed";
        ob_start();
          $attachments = realpath(get_attached_file($attach_id, true));
        get_template_part('woocommerce/emails/membership', 'activated', [
            'plan_name' => $membership_plan->name,
            'email_heading' => $subject,
            'att'=>$attachments
        ]);
        $html = ob_get_clean();
        $headers = array(
            'content-type: text/html',
        );
        //$att=$attach_id.'.'.$args['user_membership_id'];
        //$att=get_attached_file($attach_id, true);
      //  $attachments = realpath(get_attached_file($attach_id, true));
 //$attachments = array(WP_CONTENT_DIR . '/uploads/'.$att.'.png');
        wp_mail($user->user_email, $subject, $html, $headers, $attachments);
      
    }




function poker_generate_membership_qr_code($generated_data, $user_id, $size = '400x400')
    {
        require_once ABSPATH . 'wp-admin/includes/file.php';
        require_once( ABSPATH . 'wp-admin/includes/media.php' );

        $logo = poker_theme_asset('img/logo.png');

        $QR = imagecreatefrompng('https://chart.googleapis.com/chart?cht=qr&chld=H|1&chs=' . $size . '&chl=' . urlencode($generated_data));
        if ($logo !== FALSE) {
            $logo = imagecreatefromstring(file_get_contents($logo));

            $QR_width = imagesx($QR);
            $QR_height = imagesy($QR);

            $logo_width = imagesx($logo);
            $logo_height = imagesy($logo);

            // Scale logo to fit in the QR Code
            $logo_qr_width = $QR_width / 3;
            $scale = $logo_width / $logo_qr_width;
            $logo_qr_height = $logo_height / $scale;

            imagecopyresampled($QR, $logo, $QR_width / 3, $QR_height / 3, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
        }
        
        $image_file_path = wp_upload_dir()['basedir'] . "/" . uniqid($user_id . time(), true) . ".png";
      
        imagepng($QR, $image_file_path);
        imagedestroy($QR);

        $file_path = $image_file_path;
        $file_name = basename($file_path);
        $file_type = wp_check_filetype($file_name, null);
        $attachment_title = sanitize_file_name(pathinfo($file_name, PATHINFO_FILENAME));
        $wp_upload_dir = wp_upload_dir();

          $post_info = array(
            'guid'           => $wp_upload_dir['url'] . '/' . $file_name,
            'post_mime_type' => $file_type['type'],
            'post_title'     => $attachment_title,
            'post_content'   => '',
            'post_status'    => 'inherit',
        );

        // Create the attachment
        $attach_id = wp_insert_attachment($post_info, $image_file_path, null);
             
        // Include image.php
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        
        


        // Define attachment metadata
        $attach_data = wp_generate_attachment_metadata($attach_id, $file_path);
    
        // Assign metadata to attachment
        wp_update_attachment_metadata($attach_id,  $attach_data);

       
        return $attach_id;
    }

0 个答案:

没有答案
相关问题