免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6908 | 回复: 6
打印 上一主题 下一主题

[FastDFS] 做了个 PHP 客户端包装类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-25 17:58 |只看该作者 |倒序浏览
根据 PHP 客户端的实例做了个简单的包装,在我的机子上测试过了
类文件名: FastDFS.php ,放在 php 客户端的那个文件夹中

  1. <?php
  2. require 'fdfs_common.php';
  3. require 'fdfs_tracker_client.php';
  4. require 'fdfs_storage_client.php';

  5. class FastDFS
  6. {
  7.         private $tracker_server;
  8.         private $storage_server;
  9.         private $group_name;
  10.        
  11.         public function factory($group_name)
  12.         {
  13.                 static $obj;
  14.                
  15.                 if(!$obj)
  16.                         $obj = new FastDFS($group_name);
  17.                        
  18.                 return $obj;
  19.         }
  20.        
  21.         public function __construct($group_name)
  22.         {
  23.                 $this->storage_server = null;
  24.                 $this->group_name = $group_name;
  25.                
  26.                 $this->connection();
  27.         }
  28.        
  29.         public function connection()
  30.         {
  31.                 $tracker_server = tracker_get_connection();
  32.                 if ($tracker_server == false)
  33.                 {
  34.                         return $this->halt("tracker_get_connection fail\n");
  35.                 }
  36.                
  37.                 $this->tracker_server = $tracker_server;
  38.         }
  39.        
  40.         /**
  41.          * 根据文件名上传
  42.          *
  43.          * @param string $local_filename
  44.          * @param array $meta_list
  45.          * @return remote filename
  46.          */
  47.         public function upByFileName($local_filename, $meta_list=array())
  48.         {
  49.                 //$local_filename = 'nginx-logo.png';
  50.                 //$group_name = '';   //you can specify the group to upload file to
  51.                 //$meta_list = array('width' => 1024, 'height' => 768, 'color' => '#c0c0c0');
  52.                
  53.                 $result = storage_upload_by_filename($this->tracker_server, $this->storage_server,
  54.                                         $local_filename, $meta_list,
  55.                                         $this->group_name, $remote_filename);
  56.                 if ($result == 0)
  57.                 {
  58.                         //echo "group_name=$group_name, remote_filename=$remote_filename\n";
  59.                         return $remote_filename;
  60.                 }
  61.                 else
  62.                 {
  63.                         return $this->halt("storage_upload_by_filename fail, result=$result\n");
  64.                 }
  65.         }
  66.        
  67.         /**
  68.          * 把文件 buff 上传
  69.          *
  70.          * @param string $file_buff
  71.          * @param string $file_ext
  72.          * @param array $meta_list
  73.          * @return remote filename
  74.          */
  75.         public function upByBuff($file_buff, $file_ext, $meta_list=array())
  76.         {
  77.                 if ($file_buff != false)
  78.                 {
  79.                         $file_size = strlen($file_buff);
  80.                         //$group_name = '';  //you can specify the group to upload file to
  81.                         $result = storage_upload_by_filebuff($this->tracker_server, $this->storage_server,
  82.                                 $file_buff, $file_size, $file_ext, $meta_list,
  83.                                 $this->group_name, $remote_filename);
  84.                        
  85.                         if ($result == 0)
  86.                         {
  87.                                 //echo "group_name=$group_name, remote_filename=$remote_filename\n";
  88.                                 return $remote_filename;
  89.                         }
  90.                         else
  91.                         {
  92.                                 return $this->halt("storage_upload_by_filename fail, result=$result\n");
  93.                         }
  94.                 }
  95.         }
  96.        
  97.         /**
  98.          * 下载到本地文件
  99.          *
  100.          * @param string $remote_filename 远程文件名
  101.          * @param string $local_filename 本地文件名
  102.          * @return bool
  103.          */
  104.         public function downToFile($remote_filename, $local_filename)
  105.         {
  106.                 //$remote_filename = 'M00/00/00/fWaowEoaQj8AAFMnYnV13w36.php';

  107.                 $result = tracker_query_storage_fetch($this->tracker_server, $this->storage_server,
  108.                                 $this->group_name, $remote_filename);
  109.                 if ($result == 0)
  110.                 {
  111.                         //echo "storage server ${storage_server['ip_addr']}:${storage_server['port']}\n <br />";
  112.                 }
  113.                 else
  114.                 {
  115.                         return $this->halt("tracker_query_storage_fetch fail, errno: $result\n");
  116.                 }
  117.                
  118.                 $result = storage_download_file_to_file($this->tracker_server, $this->storage_server,
  119.                                 $this->group_name, $remote_filename, $local_filename, $file_size);
  120.                 if ($result == 0)
  121.                 {
  122.                         //echo "download file to file success, file size: $file_size\n <br />";
  123.                         return true;
  124.                 }
  125.                 else
  126.                 {
  127.                         return $this->halt("storage_download_file_to_file fail, errno: $result\n");
  128.                 }
  129.         }
  130.        
  131.         /**
  132.          * 下载到 buff
  133.          *
  134.          * @param string $remote_filename 远程文件名
  135.          * @return file name
  136.          */
  137.         public function downToBuff($remote_filename)
  138.         {
  139.                 $result = storage_download_file_to_buff($this->tracker_server, $this->storage_server,
  140.                 $this->group_name, $remote_filename, $file_buff, $file_size);
  141.                 if ($result == 0)
  142.                 {
  143.                         //echo "download file to buff success, file size: $file_size" . ", buff size:" . strlen($file_buff) . "\n <br />";
  144.                         $fname = str_replace('/', '_', $remote_filename);
  145.                         file_put_contents($fname, $file_buff);
  146.                         return $fname;
  147.                 }
  148.                 else
  149.                 {
  150.                         return $this->halt("storage_download_file_to_buff fail, errno: $result\n");
  151.                 }
  152.         }
  153.        
  154.         public function downToFileEx($remote_filename)
  155.         {
  156.                 $local_filename = str_replace('/', '-', $remote_filename);
  157.                 $fp = fopen($local_filename, 'wb');
  158.                 if ($fp === false)
  159.                 {
  160.                         die("open file \"$local_filename\" to write fail");
  161.                 }
  162.                 else
  163.                 {
  164.                         $result = storage_download_file_ex($this->tracker_server, $this->storage_server,
  165.                                 $this->group_name, $remote_filename, 'write_file_callback', $fp, $file_size);
  166.                         if ($result == 0)
  167.                         {
  168.                                 return $local_filename;
  169.                                 //echo "download file to file success, file size: $file_size\n <br />";
  170.                         }
  171.                         else
  172.                         {
  173.                                 return $this->halt("storage_download_file_to_file fail, errno: $result\n");
  174.                         }
  175.                         fclose($fp);
  176.                 }
  177.         }
  178.        
  179.         /**
  180.          * 删除文件
  181.          *
  182.          * @param string $remote_filename 远程文件名
  183.          * @return bool
  184.          */
  185.         public function delFile($remote_filename)
  186.         {
  187.                 $result = storage_delete_file($this->tracker_server, $this->storage_server, $this->group_name, $remote_filename);
  188.                
  189.                 return ($result==0) ? true : false;
  190.         }
  191.        
  192.        
  193.         public function halt($msg)
  194.         {
  195.                 echo "<p>{$msg}</p>";
  196.                 return false;
  197.         }
  198.        
  199.         public function disconnection()
  200.         {
  201.                 fdfs_quit($this->tracker_server);
  202.                 tracker_close_all_connections();
  203.         }
  204.        
  205.         public function __destruct()
  206.         {
  207.                 $this->disconnection();
  208.         }
  209.        
  210. }



  211. function write_file_callback($arg, $file_size, $file_buff, $buff_bytes)
  212. {
  213.         if (fwrite($arg, $file_buff, $buff_bytes) != $buff_bytes)
  214.         {
  215.                 echo "in write_file_callback fwrite fail";
  216.                 return FDFS_EIO;
  217.         }
  218.        
  219.         return 0;
  220. }
  221. ?>
复制代码



调用的例子:

  1. <?php
  2. require("FastDFS.php");

  3. //download
  4. echo FastDFS::factory('group1')->downToFile('M00/00/00/fWaowEoaUtwAADwWZmBRJw73.png', 'nginx.png'), "<br />";
  5. echo FastDFS::factory('group1')->downToFileEx('M00/00/00/fWaowEoaUtwAADwWZmBRJw73.png'), "<br />";
  6. echo FastDFS::factory('group1')->downToBuff('M00/00/00/fWaowEoaUtwAADwWZmBRJw73.png'), "<br />";

  7. //delete
  8. FastDFS::factory('group1')->delFile('M00/00/00/fWaowEoaUtwAADwWZmBRJw73.png');

  9. //upload
  10. echo FastDFS::factory('group1')->upByFileName('nginx.png'), "<br />";
  11. echo FastDFS::factory('group1')->upByBuff( file_get_contents('nginx.png'), 'png' ), "<br />";

  12. ?>
复制代码

论坛徽章:
4
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11IT运维版块每日发帖之星
日期:2016-08-11 06:20:00IT运维版块每日发帖之星
日期:2016-08-15 06:20:00
2 [报告]
发表于 2009-05-25 18:15 |只看该作者

回复 #1 snkzhong 的帖子

不错,顶一个!

论坛徽章:
0
3 [报告]
发表于 2009-05-26 13:13 |只看该作者
那是相当的不错呀。

论坛徽章:
0
4 [报告]
发表于 2009-05-26 17:49 |只看该作者
不错,学习~~~~~

论坛徽章:
0
5 [报告]
发表于 2010-03-02 09:38 |只看该作者
谢谢,不过还不是很熟悉这个……

论坛徽章:
0
6 [报告]
发表于 2011-12-01 15:26 |只看该作者
require 'fdfs_common.php';

require 'fdfs_tracker_client.php';

require 'fdfs_storage_client.php';


这三个文件当前目录下面没有

论坛徽章:
4
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:56:11IT运维版块每日发帖之星
日期:2016-08-11 06:20:00IT运维版块每日发帖之星
日期:2016-08-15 06:20:00
7 [报告]
发表于 2011-12-01 16:07 |只看该作者
那是很老的版本了,最开始直接用纯php实现的php client API。
FastDFS从v1.19以后改成了php扩展模块方式,更加简洁高效!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP