I am trying to develop a method in my admin panel that will allow for the creation of a CSV file within the plugin directory Here is my code: where $amount is the next increment of filename calculated beforehand (if 1.csv and 2.csv exist, then create 3.csv) this calculation works fine as I have tried echoing the filepath

    function createNewGallery($amount)
    $files = $amount;
    $databaseURL = plugins_url( '/', __FILE__ );
    $databaseURL .= $files + 1 . '.csv';
    echo $databaseURL;
    $headers = array('imageid', 'imgurl', 'IMGTEXT');
    echo " $headers ";

    $fp = fopen($databaseURL, 'w');
    fwrite($fp, $headers);


The code above seems to execute but doesn't actually do anything. I want it to create the next iteration of the CSV in the directory specified and then push the headers to it, but looking at the live FTP nothing is happening

  • 1
    not sure what you are trying to do, but you can not write to a URL, an you should not be able to write to plugins directory Dec 12, 2016 at 17:16

2 Answers 2


If you plan to write to a pugin folder since you used fopen, fwite, flose you are using the wrong function:

File: wp-includes/link-template.php
3189: /**
3190:  * Retrieves a URL within the plugins or mu-plugins directory.
3191:  *
3192:  * Defaults to the plugins directory URL if no arguments are supplied.
3193:  *
3194:  * @since 2.6.0
3195:  *
3196:  * @param  string $path   Optional. Extra path appended to the end of the URL, including
3197:  *                        the relative directory if $plugin is supplied. Default empty.
3198:  * @param  string $plugin Optional. A full path to a file inside a plugin or mu-plugin.
3199:  *                        The URL will be relative to its directory. Default empty.
3200:  *                        Typically this is done by passing `__FILE__` as the argument.
3201:  * @return string Plugins URL link with optional paths appended.
3202:  */
3203: function plugins_url( $path = '', $plugin = '' ) {

You should probable use this function:

File: wp-includes/plugin.php
703: /**
704:  * Get the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in.
705:  *
706:  * @since 2.8.0
707:  *
708:  * @param string $file The filename of the plugin (__FILE__).
709:  * @return string the filesystem path of the directory that contains the plugin.
710:  */
711: function plugin_dir_path( $file ) {
712:    return trailingslashit( dirname( $file ) );
713: }

Then you are on your own:

$plugin_dir = plugin_dir_path( __FILE__ );
$logfile = $plugin_dir . 'log.csv';

$fp = fopen($logfile, 'w');
fwrite($fp, $data);

At the moment plugins in WordPress have all the rights WordPress have when writing to a file system, and these are the rights given from the web server process.

Your naming convention is not perfect: $databaseURL is probably bad chosen. This is not a URL.

  • Apologies for being two years late in accepting your answer, but thank you very much! Have a great weekend. Feb 2, 2019 at 21:22

Writing to a plugin directory is not considered a proper practice:

  1. Plugin directories are overwritten on plugin updates.
  2. They are also typically made read-only for web server in more security–conscious setups.

The most easy location for write operations (as likely to be allowed as possible) in WP structure is uploads folder (wp-content/uploads by default, but should really be detected for current configuration).

Also if the data is in least bit sensitive you should consider never leaving it in web accessible folder (which is all of WP structure is by design). You could generate in different location on server and serve from there or possibly just create and send to browser on the fly, without creating intermediary file.

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.