1

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);
pzin
  • 4,200
  • 2
  • 28
  • 49
Qatest123
  • 21
  • 3
  • i have tried to connect from Norway, Sweden, and France, the server at `hideserver.com` (52.73.179.54) refuses TCP connections on port 443 (the default httpS port). can anyone else communicate with that server? – hanshenrik Nov 21 '18 at 10:26
  • @hanshenrik sorry I hide the server cause its private server, can I pm to you the server? thank you – Qatest123 Nov 21 '18 at 22:47
  • well, enable CURLOPT_VERBOSE and post the verbose log. run ```$out=fopen("php://output","wb");curl_setopt_array($ch,array(CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$out));``` before running curl_exec, what do you get? – hanshenrik Nov 21 '18 at 22:51
  • * Hostname server***.com was found in DNS cache * Trying 128.199.***.***... * Connected to server***.com (128.199.***.***) port 80 (#0) > GET /axul-display/assets/ads/ HTTP/1.1 Host: server***.com Accept: */* < HTTP/1.1 200 OK < Date: Wed, 21 Nov 2018 22:49:17 GMT < Server: Apache/2.4.29 (Ubuntu) < Vary: Accept-Encoding < Content-Length: 2854 < Content-Type: text/html;charset=UTF-8 – Qatest123 Nov 21 '18 at 23:01
  • @hanshenrik can I have your email so that I can email to you the server, thank you – Qatest123 Nov 21 '18 at 23:02
  • sure, my email is available at https://stackoverflow.com/users/1067003/hanshenrik?tab=profile – hanshenrik Nov 21 '18 at 23:07
  • @henshenrik I sent you invitation on hang out pls see thank you. – Qatest123 Nov 21 '18 at 23:12
  • @henshenrik pls see your email I already sent the server name – Qatest123 Nov 21 '18 at 23:15

3 Answers3

1

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!

Qatest123
  • 21
  • 3
0

With reference to answer posted on php - How to force download of a file?

Instead of curl call use headers to download file.

$file_name = 'ads-id-4.gif';
$file_url = 'https://hideserver.com/axul-display/assets/ads/' . 
$file_name;
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
exit;
Sangita Kendre
  • 429
  • 4
  • 11
  • OP is not trying to make php output the image to stdout, OP is trying to make the php script save the image to the system that php is running on. – hanshenrik Nov 21 '18 at 10:22
0

This worked for me..

 $ch = curl_init($url);
    if (in_array($type, ['jpg', 'jpeg','png'])) {
       curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYSTATUS, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 400);
    curl_setopt($ch, CURLOPT_FILE, fopen($temp_path, 'wb'));
    curl_exec($ch);
    $info = curl_getinfo($ch);