将文件上传到自定义文件夹wordpress

时间:2018-10-12 08:52:12

标签: wordpress file-upload upload attachment

对于此问题的答案,我进行了广泛的搜索,但未成功。 我想将文件从联系表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 condition。

我现在可以上传到自定义文件夹,但是图像的文件许可权是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 condition。

我尝试了以下上传到新目录的照片,但是照片的权限为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);
      }
     }`

1 个答案:

答案 0 :(得分:0)

这是我想出的,

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