I am trying to download an image from the internet to my local folder:
$loc = "downloaded images/".basename($_POST["url"]);
if(file_put_contents($loc,file_get_contents($_POST["url"])));
echo "Downloaded successfully";
else
echo error_get_last()['message'];
The code works only in my local server. When I ran the code in my live server, this huge warning keeps popping up in my console:
Warning: file_get_contents(https://neindiabroadcast.com/wp-content/uploads/2022/11/20221122_071752.jpg): Failed to open stream: Connection timed out in
I have also tried using cURL
:
set_time_limit(0); // unlimited max execution time
$loc = "downloaded images/".basename($_POST["url"]);
$host = $_POST["url"];
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 28800);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_REFERER, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
$fp = fopen($loc, 'wb');
fwrite($fp, $result);
fclose($fp);
When I try to open the file, it says the file is corrupted. What's surprising is that, both the codes work perfectly only when I try them in my local server.
My max_execution_time
is set to unlimited, so the time required to execute shouldn't be the issue here. Then why is this issue occurring? Please help me in resolving this issue