如何使用file_get_contents php获取图像扩展?

Ped*_*res 6 php

我正在尝试将来自其他网站的图像复制到我的服务器,我打算使用

<? 
file_put_contents($toPath, file_get_contents($fromPath));
?>
Run Code Online (Sandbox Code Playgroud)

好的,但要构建$toPath我需要文件的扩展名.我知道如果这$fromPath是一个正常的路径与文件的名称在最后,但如果我使用像facebook的路径使用http://graph.facebook.com/user_id/picture?type=large 我怎么能得到扩展?

谢谢你们.

Nie*_*sol 9

$size = getimagesize($fromPath);
$extension = image_type_to_extension($size[2]);
Run Code Online (Sandbox Code Playgroud)

获取图像大小,获取文件类型.

  • 如果您不想在扩展名之前获取点(例如:“.png”),只需添加 false 作为第二个参数:image_type_to_extension($size[2], false); (2认同)

kon*_*ddy 5

在PHP 5.3中你可以使用finfo_file(),参见PHP参考

file_put_contents($toPath, file_get_contents($fromPath));
$handle = fopen($toPath,'r');
print finfo_file($handle);
Run Code Online (Sandbox Code Playgroud)

这也适用于非图像.