I am trying to save an image file (from a specific URL) inside a folder in my local system. This is my code:
$image_link = $_POST["url"];//Direct link to image
$split_image = pathinfo($image_link);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , $image_link);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, like Gecko) Chrome/0.A.B.C Safari/525.13");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response= curl_exec ($ch);
curl_close($ch);
$file_name = "all_backend_stuff/".$split_image['filename'].".".$split_image['extension'];
$file = fopen($file_name , 'w') or die("X_x");
fwrite($file, $response);
fclose($file);
echo $file_name;
Now although the image is being saved, when I try to open it, it shows that the image is corrupted and it's size on disk is 0 B.
How do I resolve this issue?
EDIT: I have also tried this code:
$loc = "all_backend_stuff/".basename($_POST["url"]);
file_put_contents($loc,file_get_contents($_POST["url"]));
echo $loc;
The image downloaded is still corrupted.