Always use the upload directory to store files, nothing else. There are two reasons:
- Plugin directories are replaced on plugin updates, so your custom files will be deleted.
- The upload directory is the only one where you can expect write access. For example, I'm updating my plugins via composer, and there is no write access to that directory before and after that task.
Here is a simple illustration how that might work:
$uploads = wp_get_upload_dir();
if ( $uploads['error'] ) {
// something is very wrong, stop messing here
}
else {
$my_dirname = 'your_plugin_name';
$full_path = $uploads['basedir') . "/$my_dirname";
if ( ! is_dir( $full_path ) {
if ( ! mkdir( $full_path ) ) {
// handle the case when you can't create a directory
}
}
}
Now your upload directory path is $uploads['basedir') . "/$my_dirname"
, and your URL is $uploads['baseurl') . "/$my_dirname"
.
See the wp_get_upload_dir()
documentation.