首页 > 解决方案 > 将文件上传到自定义文件夹 wordpress

问题描述

我已经广泛搜索了这个问题的答案,但没有成功。我想将 Contact Form 7 中的文件上传到根目录中的文件夹。我可以将文件上传到上传文件夹,但这并不令人满意。我找到了可以更改文件夹位置的代码,但看不到如何将附件加载到该文件中。

    $folderPath = "/candidates//candidate_id/photos/";
    mkdir(ABSPATH.$folderPath, 0777, true);
    $filename = $image_name;        
if ($filename > '') {
      require(ABSPATH . 'wp-admin/includes/admin.php');
    $wp_filetype = wp_check_filetype(basename($filename), null);
    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '' . basename( $filename ),
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
        'post_content' => '',
        'post_status' => 'inherit'
    );
    $attach_id = wp_insert_attachment($attachment, $filename, $newpostid);
            require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
    wp_update_attachment_metadata($attach_id, $attach_data);
    //Define the new Thumbnail can be also a ACF field
    update_post_meta($newpostid, "_thumbnail_id", $attach_id);
}

新目录出现,但图像仍位于上传文件夹中。我看不到在 if ($filename 条件中定义新文件夹路径的位置。

我现在可以上传到自定义文件夹,但图像的文件权限是 0400。我们该如何更改?代码如下:

        '$folderPath = "/candidates//candidate_id/photos/";
mkdir(ABSPATH.$folderPath, 0777, true);
$filename = $image_name;        
if ($filename > '') {
      require(ABSPATH . 'wp-admin/includes/admin.php');
    $wp_filetype = wp_check_filetype(basename($filename), null);
    $attachment = array(
        'guid'           => $wp_upload_dir['url'] . '' . basename( $filename ),
        'post_mime_type' => $wp_filetype['type'],
        'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
        'post_content' => '',
        'post_status' => 'inherit'
    );
    $attach_id = wp_insert_attachment($attachment, $filename, $newpostid);
            require_once(ABSPATH . 'wp-admin/includes/image.php');
    $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
    wp_update_attachment_metadata($attach_id, $attach_data);
    //Define the new Thumbnail can be also a ACF field
    update_post_meta($newpostid, "_thumbnail_id", $attach_id);
}

新目录出现,但图像仍位于上传文件夹中。我看不到在 if ($filename 条件中定义新文件夹路径的位置。

我尝试了以下上传到新目录的方法,但照片的权限为 0400。如何更改为 0644?

`

$folderPath = "/candidates/".$time."/photos/";
      mkdir(ABSPATH.$folderPath, 0755, true);
        $destination= ABSPATH.$folderPath;
        // save any attachments to a temp directory
       if(strlen($image_name) > 0 and !ctype_space($image_name)) {
       $mail_attachments = explode(" ", $image_name);
        foreach($mail_attachments as $attachment) {
        $uploaded_file_path = ABSPATH . 'wp-content/uploads/wpcf7_uploads/' . $attachment;
        $new_filepath = $destination .'/'. $attachment;
        rename($image_location, $new_filepath);
      }
     }`

标签: wordpressfile-uploaduploadattachment

解决方案


这是我想出的,

function contactform7_before_send_mail( $form_to_DB ) {
    global $wpdb;
   $form_to_DB = WPCF7_Submission::get_instance();
    if ( $form_to_DB ) 
        $formData = $form_to_DB->get_posted_data();
        $uploaded_files = $form_to_DB->uploaded_files(); // this allows you access to the upload file in the temp location
    $time = /*date("dmY")."".*/time() ;
    if (!empty($formData[Photo])){
         $image_name = $formData[Photo];
    //$image_name = $time.".jpg";
    } else {$image_name="";}
   $image_location = $uploaded_files[Photo];
    if (!empty($formData[Photo])){
    $folderPath = "/candidates/".$time."/photos/";
    } else {$folderPath= "/empty/index.php/";}
    mkdir(ABSPATH.$folderPath, 0755,true);

    $destination= ABSPATH.$folderPath;

// save any attachments to a temp directory

            $new_filepath = $destination .'/'. $image_name;
            rename($image_location, $new_filepath);
            chmod($new_filepath, 0644);

推荐阅读