0

Am trying to download an image from remote server and save it in local folder but i cannot seem to find a solution here. i have seen a couple of similar questions around but mine is a bit unique.

Solutions here are for remote links ending in image name and extension e.g www.example.com/image.jpg mine is a plain url but links to an image whose image type and name is unknown e.g. https://example.com/images/6a9b0547-3d31-4dc9-891c-3ae6ec051056. please help.

1 Answers1

0

Well, at first you have to download the file to a temporary place. Let's assume you save the file as 399443553.tmp. Use mime_content_type() function to get the mime type of the file. At last, move the file to its real location with the extension you got from mime_content_type()

Here is the documentation for mime_content_type()

Example from php.net

<?php
  echo mime_content_type('php.gif') . "\n";
?>

output

image/gif

PS. Don't be confused about the .gif in the filename. It will return the real MIME type even if it is not there

Peshraw H. Ahmed
  • 439
  • 3
  • 22
  • 1
    this helped, i downloaded the file using https://stackoverflow.com/questions/6348602/download-remote-file-to-server-with-php and managed to get the extension using your approach... thank you! – The Ambidextrous Jul 02 '18 at 05:44