CF7 Form File Upload to FTP Server

I have WP web-site on a domain wp.fotoselma.com
I am using CF7 plugin for multiple file upload. Is it possible to upload files on server using FTP? Exactly I need to make new folder for every upload. For example, if name uploads X images, i need to make folder nameX . When the second guest uploads Y images, I need to store his images to folder guestY . I think you understand me.

If you need some code from the plugin, please tell me, I will write it.

Thank you in advance.

Welcome to the site!

In the CF7 code, it lets you set a folder for storage. I am not familiar with that plugin, but, it should be easy to view the code and alter where it stores the data. FTP is not the way to go. You are using WP which has it’s own security and the tools needed to upload the files.

Normally, to create a user folder for storage, you can just use the user-id from the database. When one of your users are registered, you can create a folder using their user id from the database’s auto-indexed or auto-incremented field. If these files are photos, you might create a folder named user_images and then inside that folder whenever a new user is created, create a folder inside that one named with the user_id. This is very handy as it protects the folder from using a name and is more secure. Then, you would alter the CF7 plugin to force it to save uploaded files into the “user_images/user-id” folder.
Searching around on Google, I found this from StackOverflow that shows how to save one file. You would need to alter it to your folder structure and to loop thru all the files. Might work without seeing your code.

// We need to get the CF7 field name from FILE
$cf7_file_field_name = 'uploadyourfile'; // [file uploadyourfile]
//Do the magic the same as the refer link above
$image_name = $formData[$cf7_file_field_name];
$image_location = $uploaded_files[$cf7_file_field_name];
$image_content = file_get_contents($image_location);
$wud = wp_upload_dir();
$upload = wp_upload_bits($image_name, null, $image_content);
$chemin_final = $upload['url'];
$filename = $upload['file'];
if ($filename > '') {
    require_once(ABSPATH . 'wp-admin/includes/admin.php');
    $wp_filetype = wp_check_filetype(basename($filename), null);
    $attachment = array(
        '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);
}

So, normally you show some code and we can view it and see what needs changing. There are numerous CF7 plugin’s. Each is different. Show us where the files are uploaded now and we can help you fix it.
Also, normally, you do not allow “guests” to upload files. You would only want registered members to be allowed to do this. Therefore, when a user logs into the site, you know their user-id from the database and can use that as the folder name. Works well that way. Or, some programmers use the “username” for the folder name. This works well, too as long as there are no spaces or special characters used in their user name. “pavle93” would work fine for a folder name, but, “Ernie/Alex-?2020” would not. I think that makes sense. User-ID’s are integers and therefore easy and unique. When a user account is deleted, they folder would be deleted, too.
Hope this helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service