Wordpress create subfolder in uploads directory from plugin

时间:2017-08-04 13:11:00

标签: php wordpress upload mkdir

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.

2 个答案:

答案 0 :(得分:4)

只需使用此修改版本:

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

(仅适用于PHP 5.4 +)

更正:

  • 无需将dirname( __FILE__)作为参数传递给wp_upload_dir
  • 同样wp_upload_dir返回一个数组,因此您需要访问所需的路径
  • trailingslashit已删除附加新的尾部斜杠,因此将其从字符串中删除

同时检查file / dir用户权限以确保PHP实际上可以在那里创建一个目录。

答案 1 :(得分:0)

试试这个 -

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