admin管理员组

文章数量:1429067

I use this code to download a file from remote server to mine. The file is created and saved but in the home directory. 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);

I use this code to download a file from remote server to mine. The file is created and saved but in the home directory. 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 edited May 12, 2019 at 17:50 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Nov 5, 2013 at 5:56 DrMoskoDrMosko 8512 gold badges10 silver badges18 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

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;
}

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

本文标签: plugin developmentHow specify a directory to file when downloading it