翼度科技»论坛 编程开发 PHP 查看内容

PHP下载采集图片到本地的方法详解【可忽略ssl认证】

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
readfile和file_put_contents下载远程图片到本地
  1. <?php
  2. function download_image($pic_url)
  3. {
  4.     $time = time();
  5.     $pic_local_path = dirname(__FILE__) . '/cache';
  6.     $pic_local = $pic_local_path . '/' . $time;
  7.     if (!file_exists($pic_local_path)) {
  8.         mkdir($pic_local_path, 0777);
  9.         @chmod($pic_local_path, 0777);
  10.     }
  11.     ob_start(); //打开输出
  12.     readfile($pic_url); //输出图片文件
  13.     $img = ob_get_contents(); //得到浏览器输出
  14.     ob_end_clean(); //清除输出并关闭
  15.     file_put_contents($pic_local, $img);
  16.     return $pic_local;
  17. }
复制代码
curl下载远程图片到本地
  1. <?php
  2. $ch = curl_init();
  3. $fp=fopen('./girl.jpg', 'w');
  4. curl_setopt($ch, CURLOPT_URL, "https://img02.sogoucdn.com/app/a/100520091/20181209114105");
  5. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  6. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  8. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
  9. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  10. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  11. curl_setopt($ch, CURLOPT_FILE, $fp);
  12. $output = curl_exec($ch);
  13. $info = curl_getinfo($ch);
  14. $error = curl_error($ch);
  15. fclose($fp);
  16. $size = filesize("./girl.jpg");
  17. if ($size != $info['size_download']) {
  18.         echo "下载失败";
  19.         echo $error;
  20. } else {
  21.         echo "下载成功";
  22. }
  23. curl_close($ch);
复制代码
  1. /**
  2. * 下载远程图片到本地
  3. *
  4. * @param string $url 远程文件地址
  5. * @param string $filename 保存后的文件名(为空时则为随机生成的文件名,否则为原文件名)
  6. * @param array $fileType 允许的文件类型
  7. * @param string $dirName 文件保存的路径(路径其余部分根据时间系统自动生成)
  8. * @param int $type 远程获取文件的方式
  9. * @return json 返回文件名、文件的保存路径
  10. * @author blog.snsgou.com
  11. */
  12. function download_image($url, $fileName = '', $dirName, $fileType = array('jpg', 'gif', 'png'), $type = 1)
  13. {
  14.     if ($url == '')
  15.     {
  16.         return false;
  17.     }
  18.     // 获取文件原文件名
  19.     $defaultFileName = basename($url);
  20.     // 获取文件类型
  21.     $suffix = substr(strrchr($url, '.'), 1);
  22.     if (!in_array($suffix, $fileType))
  23.     {
  24.         return false;
  25.     }
  26.     // 设置保存后的文件名
  27.     $fileName = $fileName == '' ? time() . rand(0, 9) . '.' . $suffix : $defaultFileName;
  28.     // 获取远程文件资源
  29.     if ($type)
  30.     {
  31.         $ch = curl_init();
  32.         $timeout = 30;
  33.         curl_setopt($ch, CURLOPT_URL, $url);
  34.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  35.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  36.         $file = curl_exec($ch);
  37.         curl_close($ch);
  38.     }
  39.     else
  40.     {
  41.         ob_start();
  42.         readfile($url);
  43.         $file = ob_get_contents();
  44.         ob_end_clean();
  45.     }
  46.     // 设置文件保存路径
  47.     //$dirName = $dirName . '/' . date('Y', time()) . '/' . date('m', time()) . '/' . date('d', time());
  48.     $dirName = $dirName . '/' . date('Ym', time());
  49.     if (!file_exists($dirName))
  50.     {
  51.         mkdir($dirName, 0777, true);
  52.     }
  53.     // 保存文件
  54.     $res = fopen($dirName . '/' . $fileName, 'a');
  55.     fwrite($res, $file);
  56.     fclose($res);
  57.     return array(
  58.         'fileName' => $fileName,
  59.         'saveDir' => $dirName
  60.     );
  61. }
复制代码
PHP读写大 二进制 文件

不必申请很大内存(fopen、fread、fwrite、fclose)
  1. <?php
  2. /**
  3. * 读写大二进制文件,不必申请很大内存
  4. * 只有读取到内容才创建文件
  5. * 保证目录可写
  6. *
  7. * @param string $srcPath 源文件路径
  8. * @param string $dstPath 目标文件路径
  9. * @return bool
  10. */
  11. function fetch_big_file($srcPath, $dstPath)
  12. {
  13.     set_time_limit(0); // 设置脚本执行时间无限长
  14.     if (!$fpSrc = fopen($srcPath, "rb"))
  15.     {
  16.         return false;
  17.     }
  18.     $isWriteFileOpen = false; // 写文件 是否已打开?
  19.     do
  20.     {
  21.         $data = fread($fpSrc, 8192); // 每次读取 8*1024个字节
  22.         if (!$data)
  23.         {
  24.             break;
  25.         }
  26.         else if (!$isWriteFileOpen)
  27.         {
  28.             // 第一次读取文件,并且有内容,才创建文件
  29.             $fpDst = fopen($dstPath, "wb");
  30.             $isWriteFileOpen = true;
  31.             fwrite($fpDst, $data);
  32.         }
  33.         else
  34.         {
  35.             // 写入
  36.             fwrite($fpDst, $data);
  37.         }
  38.     } while (true);
  39.     fclose($fpSrc);
  40.     fclose($fpDst);
  41.     return true;
  42. }
  43. $srcPath = 'd:/big.pdf';
  44. $dstPath = 'Z:/big.pdf';
  45. fetch_big_file($srcPath, $dstPath);
  46. echo 'success';
复制代码
注:代码说明
  1. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  2. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
复制代码
可忽略ssl认证,对于图片https协议失效的情况可以忽略验证,正常访问。
同样的,使用
  1. file_get_contents
复制代码
函数忽略https认证的话可以使用如何代码实现:
  1. $url = 'https://img02.sogoucdn.com/app/a/100520091/20181209114105';
  2. $stream_opts = [
  3.     "ssl" => [
  4.         "verify_peer"=>false,
  5.         "verify_peer_name"=>false,
  6.     ]
  7. ];
  8. $cdata = file_get_contents($url,false, stream_context_create($stream_opts));
复制代码
来源:https://www.jb51.net/program/291529xf2.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具