1

This has been bugging me for a couple of weeks now and everything I've found on it so far talks about the mimetype or content headers to get the file extension.

The URL of my images is build like this: "https://www.gravatar.com/avatar/4a042b8382a008d344561c8301509f3a?s=32&d=identicon&r=PG" (not the URL I use, but it's build the same)

    $file_info = system_retrieve_file('https://www.gravatar.com/avatar/4a042b8382a008d344561c8301509f3a?s=32&d=identicon&r=PG', 'public://Vehicles/', TRUE);

This saves the image, but without extension. I also have no idea how to edit the name of the image without loading it up again. A dpm() from $file_info says the mime-type is 'application/octet-stream'.

If I manually add an extension to the saved image (.png or .jpg or whatever) it works for any extension.

So at this point I'm happy if I can add any extension to the image programmatically, get the file into file_managed and save it to a node field.

I'm using Drupal 7.

2
  • 1
    Everything you've found is accurate :) You need to either save the file and inspect its mime type, thereby identifying the extension, then rename it, or examine the headers of the file as it's being downloaded, and identify it from there. The URL you're fetching doesn't actually have an extension, and Drupal won't try to infer one. If you know what the extension will be ahead of time, you can specify a filename to system_retrieve_file with it appended. Otherwise I think you're left with the more manual approach
    – Clive
    Sep 8, 2015 at 0:10
  • Hi @Clive, how can you you can specify a filename to system_retrieve_file with it appended? Aug 27, 2018 at 8:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.