Reputation: 252
I'm trying to curl image using his code. But it works for some images links but images from wikipedia/wikimedia aren't working. e.g. https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Johan_van_der_Veeken.jpg/266px-Johan_van_der_Veeken.jpg
Code:
<form method="post">
<input type="text" name="image_url">
<input type="submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$image =$_POST["image_url"];
$path_parts = pathinfo($image);
$rename=$path_parts['filename'];
$extension=$path_parts['extension'];
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("dowloads/$rename.$extension",'w');
fwrite($fp, $rawdata);
fclose($fp);
echo "$rename.jpg";
}
?>
Error:
When I try to open file in windows photo viewer it says can't display this picture because the file is empty.
Upvotes: 4
Views: 1741
Reputation: 7918
Since you are trying to grab the images from http(s) link you have to do things like:
//Init curl
$ch = curl_init ($url);
//Required to be false
curl_setopt($ch, CURLOPT_HEADER, 0);
//Required for http(s)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//Required to be true
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
//Response data
$rawdata=curl_exec($ch);
//close
curl_close ($ch);
Note: Its an terrible approach, to ignore SSL warnings. Instead you should set up the certificate and download, forcing the SSL if available
Upvotes: 4