Symfony调用FastDFS

  1. 我写过的package

  2. 文档以下内容与[2233466866/FastDFS]无关,如想了解[2233466866/FastDFS]用法,请看项目文档

  3. 如果是Symfony3.x,则服务文件配置如下

     # src/FastDFSBundle/Resources/config/services.yml
     services:
         file_save:
             class: FastDFSBundle\Services\FileSave
             arguments: []
    
  4. 服务文件代码实现

     <?php
    
     namespace FastDFSBundle\Services;
    
     class FileSave
     {
         private $tracker;
         private $storage;
         private $server;
    
         /**
          * 构造方法
          */
         public function __construct()
         {
             $this->tracker = fastdfs_tracker_get_connection();
             $this->storage = fastdfs_tracker_query_storage_store();
             $this->server = fastdfs_connect_server($this->storage['ip_addr'], $this->storage['port']);
         }
    
         /**
          * 从本地路径获取文件缓存
          */
         public function pathFileBuff($path)
         {
             $buff = file_get_contents($path);
             return $buff;
         }
    
         /**
          * 从url获取文件缓存
          */
         public function urlFileBuff($url)
         {
             $curl = curl_init($url);
             curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
             curl_setopt($curl, CURLOPT_ENCODING , 'gzip');
             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
             curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);
             $buff = curl_exec($curl);
             curl_close($curl);
             return $buff;
         }
    
         /**
          * 根据文件缓存获取文件mime类型
          */
         public function fileMimeType($buff)
         {
             $file_info = new \finfo(FILEINFO_MIME_TYPE);
             $mime_type = $file_info->buffer($buff);
             return $mime_type;
         }
    
         /**
          * 上传文件到FastDFS
          */
         public function uploadFile($buff, $file_ext_name, $file_other_info = [])
         {
             $push_info = fastdfs_storage_upload_by_filebuff($buff, $file_ext_name, $file_other_info);
             if(isset($push_info['group_name'])){
                 return $push_info;
             }
             return false;
         }
    
         /**
          * 从FastDFS删除文件
          */
         public function deleteFile($push_info)
         {
             $status = fastdfs_storage_delete_file($push_info['group_name'], $push_info['filename']);
             if($status == 1){
                 return true;
             }
             return false;
         }
    
         /**
          * 从FastDFS读取文件buff
          */
         public function downloadFile($push_info)
         {
             $buff = fastdfs_storage_download_file_to_buff($push_info['group_name'], $push_info['filename']);
             return $buff;
         }
    
         /**
          * 获取文件附加信息
          */
         public function fileOtherInfo($push_info)
         {
             $file_info = fastdfs_storage_get_metadata($push_info['group_name'], $push_info['filename']);
             return $file_info;
         }
    
         /**
          * 释放内存,关闭连接
          */
         public function __destruct()
         {
             fastdfs_tracker_close_all_connections();
             unset($this->tracker);
             unset($this->storage);
             unset($this->server);
         }
     }
    
  5. 如果是Symfony3.x,服务调用参考其它文档

@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-01-11 16:52:19

results matching ""

    No results matching ""