Michelle M.
Michelle M.

Reputation: 555

Wordpress create subfolder in uploads directory from plugin

I am writing a plugin that requires that I create a subfolder within the uploads directory.

Here's what I have tried thus far:

$uploads_dir = trailingslashit( wp_upload_dir( dirname( __FILE__) ) ) . '/evaluation-uploads';
wp_mkdir_p( $uploads_dir ); 

However when I check 'wp-content/uploads/' the subfolder has not been created.

Upvotes: 7

Views: 9395

Answers (2)

CodeBrauer
CodeBrauer

Reputation: 2925

Just use this modified version:

$uploads_dir = trailingslashit( wp_upload_dir()['basedir'] ) . 'evaluation-uploads';
wp_mkdir_p( $uploads_dir );

(Will only work on PHP 5.4+)

Corrections made:

  • No need to pass dirname( __FILE__) as parameter to wp_upload_dir
  • Also wp_upload_dir returns an array, so you need to access the path you need
  • trailingslashit will already remove and append a new trailing slash, so removed it from your string

Also check file/dir user-permissions to ensure PHP can actually create a dir there.

Upvotes: 11

Prince
Prince

Reputation: 186

try this-

 $upload = wp_upload_dir();
 $upload_dir = $upload['basedir'];
 $upload_dir = $upload_dir . '/mypluginfiles';
 if (! is_dir($upload_dir)) {
    mkdir( $upload_dir, 0700 );
 }

Upvotes: 1

Related Questions