How to Create a Directory in a Plugin Folder?

I am creating a plugin in WordPress Version 3.4.2. When the administrator submits a form, a new folder is created inside my plugin directory and a file saved in this new folder.

But it gives me the following error:

Read More
error : The file has not been created 

$dir = plugins_url()."/folder-name/; 

The above code returns the following path:

http://localhost/website/wp-content/plugins/abc/folder-name

mkdir($dir, 0777, true);

Related posts

Leave a Reply

3 comments

  1. Do not use the plugin directory to store new files.

    • During an update the plugin directory will be erased by WordPress. And all files in it too.
    • The plugin directory might be read-only in some setups (I do that always).

    Use the regular uploads directory for that.

    And 0777 is never a good idea. Write access for everyone is probably not what your users want.

  2. You can use plugin_dir_path in your plugin to get current path in file system.

    define( 'YOURPLUGIN_PATH', plugin_dir_path(__FILE__) );
    

    code of the function itself

    /**
     * Gets the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in
     * @package WordPress
     * @subpackage Plugin
     * @since 2.8
     *
     * @param string $file The filename of the plugin (__FILE__)
     * @return string the filesystem path of the directory that contains the plugin
     */
        function plugin_dir_path( $file ) {
            return trailingslashit( dirname( $file ) );
        }
    
  3. In short: you need a PATH, not an URL

    In long: Do not create directories in your plugin folder (see Toscho’s answer). Use the constant ´WP_CONTENT_DIR´ for the path instead of plugins_url(). This will create the directory within ´wp-content´ (on a standrad installation). Maybe you will define a sub-directory where you creates the directories.

    define( 'STORING_DIRECTORY', WP_CONTENT_DIR . '/my_plugin_storing_directory/' );
    $dir = STORING_DIRECTORY . '/folder-name/';
    

    Maybe you want to use the upload directory to create your directories. Than you should use wp_upload_dir() to get the path.

    $upload_dir = wp_upload_dir();
    $dir = $upload_dir['basedir'] . '/folder-name/';