php保存任意网络图片到服务器的方法


  本文标签:php,保存,网络图片,服务器

本文实例讲述了php保存任意网络图片到服务器的方法 。分享给大家供大家参考 。具体分析如下:

任意指定一个网络图片地址,通过这个函数下载到本地服务器

<?php
function saveImage($path) {
 if(!preg_match(/\/([^\/]+\.[a-z]{3,4})$/i,$path,$matches))
 die(Use image please);
 $image_name = strToLower($matches[1]);
 $ch = curl_init ($path);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
 $img = curl_exec ($ch);
 curl_close ($ch);
 $fp = fopen($image_name,w);
 fwrite($fp, $img);
 fclose($fp);
}
saveImage(http://www.jb51.net/images/logo.jpg);
?>

希望本文所述对大家的php程序设计有所帮助 。