Title: Uploading files to dynamically created folder
Last modified: November 2, 2018

---

# Uploading files to dynamically created folder

 *  Resolved [Doc Young](https://wordpress.org/support/users/ijyoung/)
 * (@ijyoung)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/)
 * I have researched extensively on this topic for months now and cannot find any
   code that works.
    I am trying to upload files to custom folders after form submission(
   Contact Form 7) I can make the folder at each submission but the file still uploads
   to ./uploads Here is the code /******/
 *     ```
       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];
           $image_content = file_get_contents($image_location);
       	$upload = wp_upload_bits($image_name, null, $image_content);
   
           $filename = $image_name;
       	$folderPath = "/candidates/".$time."/photos/";
       	mkdir(ABSPATH.$folderPath, 0755, true);   
   
          if ($filename > '') {
        $target_path = ABSPATH.$folderPath . "/"  ;
         move_uploaded_file($image_name, $target_path);
               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);
           }
       ```
   
    -  This topic was modified 7 years, 9 months ago by [bcworkz](https://wordpress.org/support/users/bcworkz/).
      Reason: code fixed
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fuploading-files-to-dynamically-created-folder%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [HudsonValleyWebDesign](https://wordpress.org/support/users/jaycbrf/)
 * (@jaycbrf)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10841768)
 * Sorry, for Contact form 7 development question please see their support site 
   here: [https://contactform7.com/docs/](https://contactform7.com/docs/)
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10842057)
 * HVWD is correct, you should check their docs or ask in their [dedicated support forum](https://wordpress.org/support/plugin/contact-form-7).
 * I will say though, at one time you could get the attachments uploaded through
   the contact form in the ‘wpcf7_before_send_mail’ action, extract attachment data
   and move (using rename()) the files to where ever you like. I don’t know if the
   following is still valid, but it did work at some point in the past. (credit 
   [magician11](https://wordpress.org/plugins/contact-form-7-email-verification/))
 *     ```
           // fetch the submitted form details   
           $mail_tags = $wpcf7_form->prop('mail');
           $mail_fields = wpcf7_mail_replace_tags( $mail_tags );
           $senders_email_address = $mail_fields['sender'];
   
           // save any attachments to a temp directory
           $mail_string = trim($mail_fields['attachments']);
           if(strlen($mail_string) > 0 and !ctype_space($mail_string)) {
               $mail_attachments = explode(" ", $mail_string);
               foreach($mail_attachments as $attachment) {
                   $uploaded_file_path = ABSPATH . 'wp-content/uploads/wpcf7_uploads/' . $attachment;
                   $new_filepath = WPCF7EV_UPLOADS_DIR . $attachment;
                   rename($uploaded_file_path, $new_filepath);
               }
           }
       ```
   
 * The constant `WPCF7EV_UPLOADS_DIR` would be defined as your preferred attachment
   folder. `ABSPATH` is defined by WP.
 * BTW, when you post code in these forums, please demarcate with backticks or use
   the code button. Otherwise your code is very difficult to test because it gets
   corrupted. I fixed your code for you this time 🙂
 *  Thread Starter [Doc Young](https://wordpress.org/support/users/ijyoung/)
 * (@ijyoung)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10855962)
 * Thanks BCWORKZ,
    I will give that a try. Apologies for not responding more quickly
   but as I didnt receive any notification, I hadnt realised that there were responses.
 * Best
 *  Thread Starter [Doc Young](https://wordpress.org/support/users/ijyoung/)
 * (@ijyoung)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10856317)
 * Thank you so much.
    it works!
 * You have no idea how long I have been trying to find a solution for this
 *  Thread Starter [Doc Young](https://wordpress.org/support/users/ijyoung/)
 * (@ijyoung)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10856459)
 * Small issue folks.
    The image uploads with permission 400 so cannot be read. 
   Any quick sort?
 *  Thread Starter [Doc Young](https://wordpress.org/support/users/ijyoung/)
 * (@ijyoung)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10856566)
 * Should have mentioned that new folder is in the root – not wp-content
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10857701)
 * Wouldn’t folder permissions be the same? (755 typically) It has to at least be
   writable by the PHP user, often www-data. You might need 775, depending on the
   folder’s owner. Or `chown` it to the PHP user.
 * After the file is moved (with rename), do `chmod( $new_filepath, 0644 );`
    Or
   whatever permissions you want. The leading zero and no quotes are important for
   the permission value, otherwise you get undesired, unexpected results. Permissions
   are octal (base 8) values, which is how PHP interprets numbers with leading zeros.
 * Good to know that snippet still works. Thanks for testing 🙂 Its backstory, FWIW,
   is it comes from magician11’s no longer maintained plugin to do email verification
   before sending contact form data. I helped him work out how to do this, which
   is how I knew of the snippet. At the time, CF7 underwent a major reorganization
   after which getting attachments (to hold pending email verification, since CF7
   normally sends on and quickly deletes attachments) was not clear. As you had 
   found, it’s still not clear.
 *  Thread Starter [Doc Young](https://wordpress.org/support/users/ijyoung/)
 * (@ijyoung)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10857919)
 * Interestingly no.
    the folders have 0755 but the uploaded image is only 0400.
   I tried the chmod that you sugeested but the photo folder couldnt be opened in
   Filezilla However. you have given me a thought.
 * For the record, only the rename()line of code was required to upload to new folder.
 *  Thread Starter [Doc Young](https://wordpress.org/support/users/ijyoung/)
 * (@ijyoung)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10857926)
 * Nope, that CHMOD definitely didnt work for me
 *  Thread Starter [Doc Young](https://wordpress.org/support/users/ijyoung/)
 * (@ijyoung)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10857944)
 * Hah
    Changed the line that CHMOD was on. Works
 * Thank you so much.
    Appreciate your input
 * Now to roll it out on rest of site.
 * Cheers
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10860670)
 * You’re welcome!
    I’m relieved chmod() worked, I’d be at a loss if it didn’t.

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘Uploading files to dynamically created folder’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 11 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/uploading-files-to-dynamically-created-folder/#post-10860670)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
