Skip to content
Advertisement

Corrupted image on auto download using url with curl in php

I want to download images from my server without clicking anything – when my php file executes it will download automatically, but the problem is the file is corrupt. However, when I paste the url directly in the browser and download, it works perfectly. So why does my implementation below not work? Any ideas about this?

Here’s my code:

$url_to_image = 'https://hideserver.com/axul-display/assets/ads/';
$ch = curl_init($url_to_image);
$my_save_dir = 'assets/ads/';
$filename = basename($url_to_image.'ads-id-4.gif');
$complete_save_loc = $my_save_dir . $filename;
$fp = fopen($complete_save_loc, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
curl_exec($ch);
curl_close($ch);
fclose($fp);

Advertisement

Answer

Hi everyone thanks to your help, but I solved my own problem instead of curl I used this very simple code.

$image_url = ‘http://server***.com/axul-display/assets/ads/ads-id-4.gif’;

file_put_contents('assets/ads/ads-id-4.gif', file_get_contents($image_url));

Big thanks everyone!

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement