Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get image extension using file_get_contents php?

Tags:

php

I'm trying to copy to my server an image from another site and i plan to use

<? 
file_put_contents($toPath, file_get_contents($fromPath));
?>

Ok, but to build the $toPath i need the extension of the file. I know how to do it if the $fromPath is a normal path with name of the file at the end, but if i use a path like facebook uses http://graph.facebook.com/user_id/picture?type=large how can i get the extension?

Thank you all.

like image 790
Pedro Soares Avatar asked Jan 09 '12 19:01

Pedro Soares


People also ask

What will the file_get_contents () return?

Return Values ¶ The function returns the read data or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false . Please read the section on Booleans for more information.

What is the use of file_get_contents () function?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What is the difference between file_get_contents () function and file () function?

file — Reads entire file contents into an array of lines. file_get_contents — Reads entire file contents into a string.


2 Answers

$size = getimagesize($fromPath);
$extension = image_type_to_extension($size[2]);

Get the image size, grab the file type.

like image 186
Niet the Dark Absol Avatar answered Oct 18 '22 08:10

Niet the Dark Absol


In PHP 5.3 you can use finfo_file(), see PHP ref

file_put_contents($toPath, file_get_contents($fromPath));
$handle = fopen($toPath,'r');
print finfo_file($handle);

this works also for non images.

like image 36
konsolenfreddy Avatar answered Oct 18 '22 08:10

konsolenfreddy