admin管理员组

文章数量:1122832

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 ?

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 ?

Share Improve this question edited Aug 28, 2013 at 13:08 birgire 67.8k7 gold badges119 silver badges251 bronze badges asked Aug 28, 2013 at 12:57 sureshsuresh 11 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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.

本文标签: phpCreate new folder and upload files to custom folder via wphandleupload