我正在尝试将来自其他网站的图像复制到我的服务器,我打算使用
<?
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
我怎么能得到扩展?
谢谢你们.
$size = getimagesize($fromPath);
$extension = image_type_to_extension($size[2]);
Run Code Online (Sandbox Code Playgroud)
获取图像大小,获取文件类型.
在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)
这也适用于非图像.