0
votes

I have searched extensively unsuccessfully for an answer to this one. I want to upload files from Contact Form 7 to a folder in the root. I can upload the files to the upload folder but that is not satisfactory. I found code which would change the folder location, but cannot see how to load the attachment to this file.

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

The new directory appears but the images still end up in the uploads folder. I cannot see where to define the new folder path in the if ($filename condition.

I can now upload to the custom folder but the file permission of image is 0400. How can we change that? Code as follows:

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

The new directory appears but the images still end up in the uploads folder. I cannot see where to define the new folder path in the if ($filename condition.

I have tried the following which uploads to new directory but photo has permission 0400. How can I change to 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
where is the $image_name value defined? $filename should be the path to your file, so try to find where this $image_name is defined - Angel Deykov
I didnt include that part but her is the code above $folderpath if (!empty($formData[Photo])){ //Do the magic the same as the refer link above $time = date("d-m-Y")."-".time() ; //$filename = $time."-".$filename ; } else {$time=='0';} $image_name = $formData[Photo]; $image_name = $time."-".$image_name; $image_location = $uploaded_files[Photo]; $image_content = file_get_contents($image_location); $upload = wp_upload_bits($image_name, null, $image_content); - Ian Young
For the record, the file uploads to the upload file at the moment with this script. - Ian Young
Hi folks, thought I would share the solution that I came up with. - Ian Young

1 Answers

0
votes

Here is what I came up with,

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