0

I am using wp_handle_upload to upload files from a theme template file.

if ( ! function_exists('wp_handle_upload') ) require_once( ABSPATH . 'wp-admin/includes/file.php' );
$uploadedfile = $_FILES['attachedfile'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile ) {
    //echo "File is valid, and was successfully uploaded.\n";
    //var_dump( $movefile);
     $attachedfilelink=$movefile['url'];
}

I want to create a folder inside root / uploads directory with user name of who is uploading and upload the file inside the folder.

Any help ?

0

This isn't a working example but should help,

Use wp_upload_dir() to define the default uploads folder and append a username to it using wp_get_current_user.

$wp_upload_dir =  wp_upload_dir();
$user_folder = wp_get_current_user();

// The actual folder
$custom_upload_folder= $wp_upload_dir['basedir'] . $user_folder->display_name;
//make the dir
mkdir($custom_upload_folder);

You would need to employ security and code that checks for errors/user capabilities and sets the correct file/folder permissions. Also have a look at https://codex.wordpress.org/Filesystem_API which employs mkdir.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.