Home  >  Article  >  php教程  >  php保存任意网络图片到服务器的方法,

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

WBOY
WBOYOriginal
2016-06-13 09:07:02814browse

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

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

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

<&#63;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.bkjia.com/images/logo.jpg');
&#63;>

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn