2

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:

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);
0

3 Answers 3

5

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.

0
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 ) );
    }
2
  • my path is correct, there is no problem with plugins_url() but mkdir() not creating the folder Nov 25, 2012 at 11:48
  • 1
    Do you understand that You tring to create directory on a file system with argument of URL? You need PATH of file which you use with plugin_dir_path help. defining a constant will make less code just typing $dir = YOURPLUGIN_PATH."/folder-name/; (in case if you had define your constant).. Nov 25, 2012 at 12:08
2

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/';
1
  • The upload directories are the only ones with guaranteed write access. You should not expect write access for WP_CONTENT_DIR.
    – fuxia
    Nov 25, 2012 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.