tags:

views:

291

answers:

4

Hi,

I'm having a problem with cURL. I am downloading images and saving them to a folder. The file that cURL creates has the right filesize, which makes me think that the headers are being read properly. But, when I open the file up in my browser or in any picture-viewing application, only a tiny bit at the top appears to actually have been written. My code:

function _vancore_curl_savefile($url) {
  $url = str_replace("\"", "", $url);
  $basename = basename($url);
  $basename = str_replace("%20", "_", $basename);
  $var = file_directory_path() . "/van/" . $basename;
  $uvar = "files/van/" . $basename;
  $handle = fopen($var, "w");
  $curl = curl_init($url);
  curl_setopt($curl, CURLOPT_FILE, $handle);
  $result = curl_exec($curl);
  $result2 = $result;
  curl_close($curl);
  fclose($handle);
  return $uvar;
}

file_directory_path() is a Drupal function (this function is part of a Drupal module and called for each file that needs to be downloaded) that returns the path to the Drupal file download directory. I have confirmed through various tests that:

a) $url is what it should be
b) fopen() is opening the right file
c) curl_exec() is returning true after it is executed

I am very confused about what is going wrong here. Anyone have any thoughts?

TIA,
Benjy

+1  A: 

Set:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
code_burgar
Still the same problem. I also tried just setting that flag (and not CURLOPT_FILE) and then writing the results of the operation to the file, but the same problem persisted.
benjy
+1  A: 

Try if this helps:

curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
marcvangend
+1  A: 

May be you're missing curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1); (along with CURLOPT_RETURNTRANSFER, as said above)

Here is an example.

Kniganapolke
+2  A: 

You don't happen to be using the curl library "emulation" in the Drupal curl module. Are you? That doesn't always work. Make sure you have the real PHP curl library installed.

cwegrzyn
Wow, that was it! Can't believe it was something so simple. Thanks for your help!
benjy