PHP 根据URL将图片下载到本地
2016-06-21 11:46
676 查看
根据URL下载图片到本地
<?php
function test() {
$url = 'http://wx.qlogo.cn/mmopen/PiajxSqBRaEKUmIia2JJ9rtp4BMPvbKD8wgmqeEQ5j6m2eoeu5GNaMDbGGWLib8dFdxJaUibSGZ2Vh9Q6mYnpCHpVA/0';
$header = array("Connection: Keep-Alive", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3", "User-Agent: Mozilla/5.0
(Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $v);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
$content = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
echo "string";
print_r($curlinfo);
//关闭连接
curl_close($ch);
if ($curlinfo['http_code'] == 200) {
if ($curlinfo['content_type'] == 'image/jpeg') {
$exf = '.jpg';
} else if ($curlinfo['content_type'] == 'image/png') {
$exf = '.png';
} else if ($curlinfo['content_type'] == 'image/gif') {
$exf = '.gif';
}
//存放图片的路径及图片名称 *****这里注意 你的文件夹是否有创建文件的权限 chomd -R 777 mywenjian
$filename = date("YmdHis") . uniqid() . $exf;//这里默认是当前文件夹,可以加路径的 可以改为$filepath = '../'.$filename
$res = file_put_contents($filename, $content);//同样这里就可以改为$res = file_put_contents($filepath, $content);
}
}
test();
?>
<?php
function test() {
$url = 'http://wx.qlogo.cn/mmopen/PiajxSqBRaEKUmIia2JJ9rtp4BMPvbKD8wgmqeEQ5j6m2eoeu5GNaMDbGGWLib8dFdxJaUibSGZ2Vh9Q6mYnpCHpVA/0';
$header = array("Connection: Keep-Alive", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3", "User-Agent: Mozilla/5.0
(Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $v);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
$content = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
echo "string";
print_r($curlinfo);
//关闭连接
curl_close($ch);
if ($curlinfo['http_code'] == 200) {
if ($curlinfo['content_type'] == 'image/jpeg') {
$exf = '.jpg';
} else if ($curlinfo['content_type'] == 'image/png') {
$exf = '.png';
} else if ($curlinfo['content_type'] == 'image/gif') {
$exf = '.gif';
}
//存放图片的路径及图片名称 *****这里注意 你的文件夹是否有创建文件的权限 chomd -R 777 mywenjian
$filename = date("YmdHis") . uniqid() . $exf;//这里默认是当前文件夹,可以加路径的 可以改为$filepath = '../'.$filename
$res = file_put_contents($filename, $content);//同样这里就可以改为$res = file_put_contents($filepath, $content);
}
}
test();
?>
相关文章推荐
- 一个关于if else容易迷惑的问题
- java-用HttpURLConnection发送Http请求.
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- rem : web app适配的秘密武器
- jquery高级应用之Deferred对象
- php7 读取php.ini[4]
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- VBScript 剪贴板抓取URL并在浏览器中打开
- upload上传单张图片
- 图片引发的溢出危机(图)
- C# WinForm控件对透明图片重叠时出现图片不透明的简单解决方法
- PHP+Apache在Windows 9x下的安装和配置