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.
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.
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.
file — Reads entire file contents into an array of lines. file_get_contents — Reads entire file contents into a string.
$size = getimagesize($fromPath);
$extension = image_type_to_extension($size[2]);
Get the image size, grab the file type.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With