How to redirect uploads from customers to go into specific folder?

28 views Asked by At

I am using this plugin, but I wanted to modify the php to redirect my uploads to a specific 'clientsimages' folder. However I can't seem to get it to work, any suggestions?

I'm using this code: https://github.com/ruvictor/wc-file-attribute/blob/master/att.php

This is the code I tried, but it isn't uploading my images anymore. Prior to this it was just uploading everything to my normal media library:

if( ! empty( $_FILES['vicode-file-field']["name"] ) ) {
    // WordPress environment
    require( dirname(__FILE__) . '/../../../wp-load.php' );
    
    $wordpress_upload_dir = wp_upload_dir();
    // $wordpress_upload_dir['path'] is the full server path to wp-content/uploads/2017/05, for multisite works good as well
    // $wordpress_upload_dir['url'] the absolute URL to the same folder, actually we do not need it, just to show the link to file
    $i = 1; // number of tries when the file with the same name already exists
    
    $file_image = $_FILES['vicode-file-field'];
    $new_file_path = $wordpress_upload_dir['/wp-content/uploads/clientimages'] . '/' . $file_image['name'];
    $new_file_mime = mime_content_type( $file_image['tmp_name'] );
    
    if( empty( $file_image ) )
        die( 'File is not selected.' );
    
    if( $file_image['error'] )
        die( $file_image['error'] );
    
    if( $file_image['size'] > wp_max_upload_size() )
        die( 'It is too large than expected.' );
    
    if( !in_array( $new_file_mime, get_allowed_mime_types() ) )
        die( 'WordPress doesn\'t allow this type of uploads.' );
    
    while( file_exists( $new_file_path ) ) {
        $i++;
        $new_file_path = $wordpress_upload_dir['/wp-content/uploads/clientimages'] . '/' . $i . '_' . $file_image['name'];
    }
0

There are 0 answers