I have a function that gets an image from a provided URL e.g. to download a user's image to an images folder for further reference. Below is the function:
public function _get_image_from_url($url)
{
$image_name = $this->uuid->v4().'.jpg';
$file_extension = strtolower(substr(strrchr($image_name, '.'), 1));
$path = APPPATH.'../assets/uploads/images/';
$ch = curl_init($url);
$fp = fopen($path.$image_name, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, true);
switch( $file_extension ) {
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpeg"; break;
default:
}
$headers = array(
'Content-type: '.$ctype,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
return $image_name;
}
Note: I am using CI 3 and the uuid library to generate the file name, which is saved to the database. I have confirmed curl is enabled on remote server and file permissions are good.
Problem: The function works on the local machine (Linux) but when executed on a remote server (shared hosting), the saved file is empty (and for some reason, the file type is 'jpeg-file')
Any help on solving why the executing on the remote server fails to correctly download an image will be much appreciated.