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.
答案 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 );
}