We are using cURL to download files from one of our servers to our workflow server. I'm using the simple code pasted below, but my files are "doubling up" - twice the size, twice the length. Would seem the file is saved, then appended again, but I can't figure out why in the world that is happening.
Below is test code on my local machine behaving exact same way:
$file_name = 'test.mp3'; $copy_to_dir = 'C:/test_files/'; $file_url = 'C:/source_files/test.mp3'; $fp = fopen ($copy_to_dir. '/' . $file_name, 'w'); $ch = curl_init($file_url); curl_setopt_array($ch, array( CURLOPT_URL => $file_url, CURLOPT_RETURNTRANSFER => 1, CURLOPT_FILE => $fp, CURLOPT_TIMEOUT => 50 )); $results = curl_exec($ch); if(curl_exec($ch) === false) { //echo 'Curl error: ' . curl_error($ch); return false; } curl_close($ch); fclose($fp);
Original file is 9MB, copy is 18MB every time
EDIT: let this be a lesson to you not copy/paste code when you're in a hurry, to "save time". The second curl_exec() should be a curl_errno() or other error handling strategy - even just using fwrite() meant I would be downloading everything twice, so you don't want to just quit once you get a workaround