WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

From what I can see wp_remote_get() saves remote file contents to memory.

The files that I need to download are compressed in either ZIP or GZIP and within which will either be a CVS or XMl file

What I need to do first is download the remote file to the harddrive as ZIP either GZIP and then unzip them

Is it possible to use wp_remote_get() to download the whole file and save it to a directory?

A non-Wordpress solution I used before was cURL:

public function grab_file($url, $new_file) {

    //get file
    $ch = curl_init();
    $fp = fopen(DIR_PATH."zip/$new_file", "w");

    $options = array(CURLOPT_URL => $url, CURLOPT_HEADER => 0, CURLOPT_FAILONERROR =>

    curl_setopt_array($ch, $options);
    $file = curl_exec($ch);

    if (!$file) {
        //$error = "cURL error number:" . curl_errno($ch);
        //$error .= "cURL error:" . curl_error($ch);
        return false;

    } else {

        return true;

share|improve this question

Well here's the solution that I came up with:

// Use wp_remote_get to fetch the data
$response = wp_remote_get($url);

// Save the body part to a variable
$zip = $data['body'];

// In the header info is the name of the XML or CVS file. I used preg_match to find it
preg_match("/.datafeed_([0-9]*)\../", $response['headers']['content-disposition'], $match);

// Create the name of the file and the declare the directory and path
$file = DIR_PATH."zip/"."datafeed_".$match[1].".zip";

// Now use the standard PHP file functions
$fp = fopen($file, "w");
fwrite($fp, $zip);

// to unzip the file use the Wordpress unzip_file() function
// You MUST declare WP_Filesystem() first
if (unzip_file($file, DIR_PATH."feeds")) {
// Now that the zip file has been used, destroy it
return true;
} else {
return false;

Dealing with a GZIP file was a bit different:

// It is necessary to use the raw URL and find the extension of the encluse XML or CVS file first
private function save_ungzip($data, $url, $ext) {

// As with ZIP, save the body of wp_remote_get() to memory
$gzip = $data['body'];

// As with ZIP, look for the name of the file in the header
preg_match("/.datafeed_([0-9]*)\../", $data['headers']['content-disposition'], $match);
$file = DIR_PATH."feeds/"."datafeed_".$match[1].".$ext";

// now you need to use both the PHP gzip functions and the PHP file functions
$remote = gzopen($url, "rb");
$home = fopen($file, "w");

while ($string = gzread($remote, 4096)) {
    fwrite($home, $string, strlen($string));



So in conclusion. When you use wp_remote_get() to retrieve a remote file use the inbuilt PHP file functions to then save it where you want to. If you have a solution that utilizes Wordpress functions then please post it up.

share|improve this answer
Excellent answer, and good job for coming back and answering your own question. Kudos. – MikeSchinkel Sep 11 '13 at 3:45
what happens if you download 1GB file that way? – Andy Jun 1 '15 at 13:44
In that case we need to use 'stream' = true and 'filename' in the wp_remote_get args. – Wiliam Nov 15 '15 at 20:03

Your Answer


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.