【问题标题】:Upload file to custom folder wordpress将文件上传到自定义文件夹 wordpress
【发布时间】:2019-03-17 11:07:51
【问题描述】:

我已经广泛搜索了这个问题的答案,但没有成功。 我想将 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);
      }
     }`

【问题讨论】:

  • $image_name 值在哪里定义? $filename 应该是你的文件的路径,所以试着找到这个 $image_name 是在哪里定义的
  • 我没有包括那部分,但她是上面的代码 $folderpath if (!empty($formData[Photo])){ //和上面的引用链接一样魔术 $time = date ("dmY")."-".time() ; //$filename = $time."-".$filename ; } else {$time=='0';} $image_name = $formData[照片]; $image_name = $time."-".$image_name; $image_location = $uploaded_files[照片]; $image_content = file_get_contents($image_location); $upload = wp_upload_bits($image_name, null, $image_content);
  • 备案,文件上传到上传文件的时刻,用这个脚本。
  • 大家好,我想我会分享我想出的解决方案。

标签: wordpress file-upload upload attachment


【解决方案1】:

这是我想出的,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多