Php远程获取图片类

PHP代码
  1. if ( ! defined('BASEPATH')) exit('No direct script access allowed');  
  2. /* 
  3.  * 远程获取图片类 
  4.  *  
  5.  * 要求开启curl扩展 
  6.  * 模拟php上传原理,创建一个缓存目录,将远程获取的文件存放到缓存目录下.  
  7.  *  
  8.  *  
  9.  */  
  10.   
  11. class url_pic{  
  12.       
  13.     protected $cache//缓存路径  
  14.       
  15.     public function  __construct($cache='')  
  16.     {  
  17.         if(!emptyempty($cache))  
  18.         {  
  19.             $this->cache = $cache;  
  20.         }  
  21.         else  
  22.         {  
  23.             $this->cache = 'uploads/cache/';  
  24.         }  
  25.     }  
  26.       
  27.     //设置缓存目录  
  28.     public function set_cache($cache='')  
  29.     {  
  30.         if(!emptyempty($cache))  
  31.         {  
  32.             $this->cache = $cache;  
  33.         }  
  34.     }  
  35.     /* 
  36.      * 获取远程图片 将文件存入cache文件夹 
  37.      *  
  38.      * $url 获取远程的文件的链接 
  39.      * $error  
  40.      * @return 777 则返回不能建立文件夹  
  41.      * @return 存入缓存的文件名 
  42.      */  
  43.     public function get_file($url,$error=777)  
  44.     {  
  45.             $path = $this->build_folder($this->cache);  
  46.             if($path==false) return $error;  
  47.               
  48.             $curl = curl_init();  
  49.             // 设置你需要抓取的URL  
  50.             curl_setopt($curl, CURLOPT_URL, $url);  
  51.             // 设置header  
  52.             curl_setopt($curl, CURLOPT_HEADER, 0);  
  53.             // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。  
  54.             curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
  55.             // 运行cURL,请求网页  
  56.             $file = curl_exec($curl);  
  57.             // 关闭URL请求  
  58.             curl_close($curl);  
  59.               
  60.             //将文件写入获得的数据  
  61.             $filename = $this->cache.date("YmdHis");  
  62.             if(self::build_file($file$filename)==false)  
  63.             {  
  64.                 return false;  
  65.             }  
  66.             return $filename;  
  67.     }  
  68.       
  69.     //建立文件夹  
  70.     public function build_folder($dir)  
  71.     {  
  72.         if (!is_dir($dir))  
  73.         {  
  74.             if (!mkdir($dir,0777,TRUE) || !chmod($dir,0777))  
  75.             {  
  76.                     return false;  
  77.             }  
  78.         }  
  79.         return true;  
  80.     }  
  81.       
  82.     /* 
  83.      * 移动文件 模拟php的move_uploaded_file方法 
  84.      *  
  85.      * $cache 缓存文件路径 
  86.      * $filename 需要生成的文件名的绝对路径 
  87.      *  
  88.      * @return $filename 
  89.      */  
  90.     public function move_file($cache,$filename)  
  91.     {  
  92.         $file = @file_get_contents($cache);  
  93.         if(self::build_file($file$filename)==false)  
  94.         {  
  95.             return false;  
  96.         }  
  97.         unlink($cache); //清除缓存图片  
  98.         return $filename;  
  99.     }  
  100.       
  101.     /* 
  102.      * 生成文件 
  103.      * $file 需要写入的文件或者二进制流 
  104.      * $newname 需要生成的文件名的绝对路径 
  105.      */  
  106.     protected static function build_file($file,$filename)  
  107.     {  
  108.         $write = @fopen($filename,"w");  
  109.         if($write==false)  
  110.         {  
  111.             return false;  
  112.         }  
  113.         if(fwrite($write,$file)==false)  
  114.         {  
  115.             return false;  
  116.         }  
  117.         if(fclose($write)==false)  
  118.         {  
  119.             return false;  
  120.         }  
  121.         return true;   
  122.     }  
  123.   
  124. }  
 


上一篇: Php中curl应用实例
下一篇: PHP中使用curl取得HTTP状态码
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: php
相关日志:
评论: 0 | 引用: 0 | 查看次数: 2323
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭

 广告位

↑返回顶部↑