1

I have working code that uploads an image from the front end and sets it as a featured image and custom field photo for a custom post type but I am unable to figure out how to set the upload path.

Essentially if I have an employee I would like to use the custom field last_first to generate a folder path:

"wp-content/uploads/employee_mug/[last_first]/"

which would contain a photo of the employee. I require this as the folder structure is required for another page. The code is in my functions.php file but is simply in an if statement.

I've attempted to use move_uploaded_file with no success.

if (isset($_POST['last_first']) && is_user_logged_in()){

    $my_post = array (
        'post_type' => 'employee',
        'post_status' => 'publish', // can also draft, private or  publish
        'post_title' => $_POST['last_first'],
    );

    $postID = wp_insert_post($my_post);

    if (!function_exists('wp_generate_attachment_metadata')){
        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
    }
    if ($_FILES) {
        foreach ($_FILES as $file => $array) {
            /*
            if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                return "upload error : " . $_FILES[$file]['error'];
            }
            */
            $attach_id = media_handle_upload( $file, $postID );
        }   
    }
    if ($attach_id > 0){
        //and if you want to set that image as Post  then use:
        update_post_meta($postID,'_thumbnail_id',$attach_id);
    }

    //update_field('whatever_field_key_for_venue_field', $_POST['venue'], $postID);
    update_field('last_first', $_POST['last_first'], $postID);
    update_field('employee_mug', $attach_id, $postID);
    update_field('date_of_birth', $_POST['dob'], $postID);
    update_field('sex', $_POST['sex'], $postID);


    die;
}

I would like that when a file is attached the end result be:

"wp-content/uploads/employee_mug/[last_first]/1.jpg"

UPDATE 1.0

if ($_FILES) {
        foreach ($_FILES as $file => $array) {
            /*
            if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                return "upload error : " . $_FILES[$file]['error'];
            }
            */
            $path = "./wp-content/uploads/mug_shots/";
            $path .= $name;
            if ( wp_mkdir_p( $path ) ) {
                $attach_id = media_handle_upload( $file, $postID );
                move_uploaded_file($file, $path);
            } else {
            wp_mkdir_p( $path );
            $attach_id = media_handle_upload( $file, $postID );
            move_uploaded_file($file, $path);
            }
        } 

now checks to see if a folder was create and if not it will make one with the employee name but the move_uploaded file is not moving the photo into the generator folder. I simply need to find a way to make the file move to the generated folder at this point, Any help would be greatly appreciated!

UPDATE 2.0

the bellow code moves the file to wp-content/uploads/employee_mug/ but will not move it into the generated folder with the employee is name. I'm very close here I simply need the file moved into the employee is named folder at this point!

$name = $_POST['last_first'];

if ($_FILES) {
        foreach ($_FILES as $file => $array) {
            /*
            if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
                return "upload error : " . $_FILES[$file]['error'];
            }
            */

            //for MKDIR FOR CREATION
            $path = "./wp-content/uploads/employee_mug/";
            $path .= $name;

            //FOR MOVING UPLOADED IMAGE
            function my_upload_dir($upload) {

              $upload['subdir'] = '/employee_mug/' . $name;

              $upload['path']   = $upload['basedir'] . $upload['subdir'];

              $upload['url']    = $upload['baseurl'] . $upload['subdir'];

              return $upload;

            }

            if ( wp_mkdir_p( $path ) ) {

                if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
                $uploadedfile = $_FILES['mug_shot'];
                $upload_overrides = array( 'subjects_add' => false );
                add_filter('upload_dir', 'my_upload_dir');
                $attach_id = media_handle_upload( $file, $postID );
                remove_filter('upload_dir', 'my_upload_dir');

                /*
                move_uploaded_file($file, $path);
                $attach_id = media_handle_upload( $file, $postID );
                */

            } else {

                wp_mkdir_p( $path );

                if ( ! function_exists( 'wp_handle_upload' ) ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
                $uploadedfile = $_FILES['mug_shot'];
                $upload_overrides = array( 'subjects_add' => false );
                add_filter('upload_dir', 'my_upload_dir');
                $attach_id = media_handle_upload( $file, $postID );
                remove_filter('upload_dir', 'my_upload_dir');

                /*
                move_uploaded_file($file, $path);
                $attach_id = media_handle_upload( $file, $postID );
                */
            }
        }   
    }
    if ($attach_id > 0){
        //and if you want to set that image as Post  then use:
        update_post_meta($postID,'_thumbnail_id',$attach_id);
    }

////////////////////////////////////////////////////////////////////////////////////////////    
    //update_field('whatever_field_key_for_venue_field', $_POST['venue'], $postID);
    update_field('last_first', $_POST['last_first'], $postID);
    update_field('employee_mug', $attach_id, $postID);

UPDATE 3.0 SOLVED ******

function my_upload_dir($upload) {

              $upload['subdir'] = '/employee_mug/' . $_POST['last_first'];

              $upload['path']   = $upload['basedir'] . $upload['subdir'];

              $upload['url']    = $upload['baseurl'] . $upload['subdir'];

              return $upload;

            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.