I'm trying to get a file/image downloaded to a folder on my first server, from second server. I have the following code:
$image = 'http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg';
$imageName = pathinfo( $image, PATHINFO_BASENAME );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $image );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$source = curl_exec( $ch );
curl_close( $ch );
file_put_contents( './content/' . $imageName, $source );
$out = preg_split('/(\r?\n){2}/', $source, 2);
$headers = $out[0];
$headersArray = preg_split('/\r?\n/', $headers);
$headersArray = array_map(function($h) {
return preg_split('/:\s{1,}/', $h, 2);
}, $headersArray);
$tmp = [];
foreach($headersArray as $h) {
$tmp[strtolower($h[0])] = isset($h[1]) ? $h[1] : $h[0];
}
print_r($tmp);
The linked url is not of my servers, but works as expected, writing a file on my first server. But when I use my own second server link, for example https://example.com/demo1.png, it writes a file of 0 bytes.
On logging out the headers, the external link which is not of my server logs out an array of many items about the image such as "content-type" and "content-length". On logging out the header response of the image of my second server, it logs out an empty array..
What adjustments do I need to do on the script? I'm also okay to do some adjustments on my second server(?), if needed. Thank you in advance.