在 WordPress 中以编程方式将文件上传给用户

Posted

技术标签:

【中文标题】在 WordPress 中以编程方式将文件上传给用户【英文标题】:Upload files programmatically to users in WordPress 【发布时间】:2016-02-09 07:51:28 【问题描述】:

我正在使用 pods 插件向用户添加新的自定义字段,因此我添加了 4 个文件自定义字段,然后尝试从注册页面将文件上传到该字段并创建新用户。但是,当我上传文件时,它并没有生成所有图像大小,并且当从管理面板打开新用户时,图像没有显示,但名称是,并且图像帖子已创建。请提供任何帮助。

我的代码:

 $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);
                

上传文件功能:

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)) 

                $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;
        

【问题讨论】:

转到此链接可能对您有所帮助***.com/questions/34937692/… 【参考方案1】:

它只是在

中缺少$upload_dir['path'] . "/" . $upload_file_name
            $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);

【讨论】:

以上是关于在 WordPress 中以编程方式将文件上传给用户的主要内容,如果未能解决你的问题,请参考以下文章

text 在wordpress中以编程方式检查并创建新的帖子/页面

在 Woocommerce 中以编程方式添加新产品类别

是否可以在资源管理器窗口中以编程方式将文件夹添加到 Windows 10 快速访问面板?

在java中以编程方式编辑apk文件

如何在 Swift 中以编程方式打开 icloud 应用程序(文件)

在 C# 中以编程方式从 Excel 文件中大量导入数据到 Access