<?php
function getImg($url, $fileName=''){
if($url=='') die('地址参数错误');
if($fileName == ''){
$ext_name = strrchr($url, '.');
$allow_ext = ['.gif', '.jpg', '.gif', '.jpeg'];
if(!in_array($ext_name, $allow_ext)) die('文件格式不对');
$new_file_name = time().$ext_name;
}
ob_start(); //打开缓存
readfile($url);//读入一个文件并写入到输出缓冲。
$img_file = ob_get_contents(); //获取缓存保存到变量
ob_end_clean();//关闭并清除缓存
$local = fopen($new_file_name, 'ab');
fwrite($local, $img_file);
fclose($local);
return $fileName;
}
echo getImg("http://www.mysqltutorial.org/wp-content/uploads/2008/05/mysqltutorial.gif");
/* 还有其他函数,比如:
file_get_contents();
file_put_contents();
*/