Home > Backend Development > PHP Tutorial > php保存任意网络图片到服务器的方法_php技巧

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

WBOY
Release: 2016-05-16 20:17:33
Original
1067 people have browsed it

本文实例讲述了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.jb51.net/images/logo.jpg');
&#63;>
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template