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); }The page I need help with: [log in to see the link]
Viewing 11 replies - 1 through 11 (of 11 total)
Viewing 11 replies - 1 through 11 (of 11 total)
The topic ‘Uploading files to dynamically created folder’ is closed to new replies.