Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use this code to download a file from remote server to mine and the file is created and saved but in the home directory and i want it in the plugin directory

$url ='*.dat';
// Use wp_remote_get to fetch the data
$response = wp_remote_get($url);

// Save the body part to a variable
$fileContent = $response['body'];


// Create the name of the file and the declare the directory and path

$file ='*.dat';

// Now use the standard PHP file functions
$fp = fopen($file, "w");
fwrite($fp, $fileContent);
fclose($fp);
share|improve this question
add comment

2 Answers

You can try this get the plugin directory:

$pluginPath = dirname(_____FILE_____); //plugin path

$pluginUrl = WP_PLUGIN_URL . '/pluginname/'; //plugin URL

good question

you just create one template in your plugin directory. there u use the above code.

you create one page and assign this template like this

add_filter( "template_include", "yourpage" ) ;

function yourpage($single_template) { global $post, $pagenow;

    if ($pagenow == 'download-template.php') {

        $single_template = dirname( __FILE__ ) . '/templates/download-template.php';
    }
    return $single_template;
}
share|improve this answer
add comment

If you are wanting to write the file to the default plugins directory; then you can use part of what @abalamurugan suggested; in combination with your existing code:

$url ='*.dat';
$response = wp_remote_get($url);
$fileContent = $response['body'];

$pluginUrl = WP_PLUGIN_URL; // Plugins directory url
$file = $pluginUrl.'/*.dat';

$fp = fopen($file, "w");
fwrite($fp, $fileContent);
fclose($fp);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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