Code Snippet to Create a Directory Within Uploads Folder

  Introduction
Here's a code snippet that allows your plugin to create a new folder in the /uploads directory on activation. Could be useful if your plugin needs to allow imports or uploads and you want them stored in a separate directory.
WP Mayor’s work is supported by its audience. If you purchase through a link on our website, we may earn a commission. Here’s why you can trust us.
Table of contents

Here’s a code snippet that allows your plugin to create a new folder in the /uploads directory on activation. Could be useful if your plugin needs to allow imports or uploads and you want them stored in a separate directory.

[php]
function myplugin_activate() {

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

register_activation_hook( __FILE__, ‘myplugin_activate’ );
[/php]
If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

Jean Galea
Jean Galea
Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.
Jean Galea
Jean Galea
Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.
PeachPay for WooCommece

An all-in-one checkout and payment solution for WooCommerce . See your store's sales increase by 35%!

4 Months FREE with WP Engine

4 months free on all shared managed WordPress and eCommerce hosting plans. Offer ends on 12/01/2022.

Expand your toolset with expertly verified solutions delivered to your inbox every week.
Join thousands of designers, developers, and marketers who trust our experts to find and test the best tools for their WordPress sites.
By signing up to our newsletter, you agree to receive electronic communications from WP Mayor that may include advertisements.
Expand your toolset with expertly verified solutions delivered to your inbox every week.
Join thousands of designers, developers, and marketers who trust our experts to find and test the best tools for their WordPress sites.​
By signing up to our newsletter, you agree to receive electronic communications from WP Mayor that may include advertisements.

3 Responses

  1. I prefer use the WP way than the PHP way:
    wp_mkdir_p( $upload_dir );
    or more techy:
    require_once( ABSPATH . ‘wp-admin/includes/class-wp-filesystem-base.php’ );
    require_once( ABSPATH . ‘wp-admin/includes/class-wp-filesystem-direct.php’ );
    $wp_fs_d = new WP_Filesystem_Direct( new StdClass() );
    if ( !$wp_fs_d->is_dir( $upload_dir ) && !$wp_fs_d->mkdir( $upload_dir, 0705 ) )
    wp_die( sprintf( __( ‘Impossible to create %s directory.’ ), $upload_dir ) );

    By the way, WP recommand a 705 CHMOD, not 700, and take care readers: do not use “$uploads” var name this is overwritten by WordPress.

    1. Probably would personally stick to wp_mkdir_p() here. Using the Filesystem API would probably be a bit of overkill!

Leave a Reply

Your email address will not be published. Required fields are marked *

From web hosting to WooCommerce plugins and backup services, we have put together a collection of exclusive coupons and deals just for you.
WP Mayor_Icon_white
2022 Reader Survey