FastDFS的PHP客户端封装类

<?php
/**
* FastDFS封装
* @author fengyuty@126.com
*/
class fdfs {
    private $status = false;
    private $tracker = null;
    private $connect = null;
    private $storage = null;
    
    /**
     * 构造函数
     */
    public function __construct($config = null) {
        if(!extension_loaded('fastdfs_client')) {
            exit('Error fastdfs: No fastdfs client');
        }
        
        $this->tracker = fastdfs_tracker_get_connection();
        if(!$this->tracker) {
            exit("Error FastDFS: #" . fastdfs_get_last_error_no() . " " . fastdfs_get_last_error_info());
        }
        
        $this->connect = fastdfs_connect_server($this->tracker['ip_addr'], $this->tracker['port']);
        $this->storage = fastdfs_tracker_query_storage_store();
        if(!$this->storage || !$this->connect) {
            exit("Error Fastdfs: #" . fastdfs_get_last_error_no() . " " . fastdfs_get_last_error_info());
        }
        
        $this->status = true;
    }
    
    /**
     * 销毁函数
     */
    public function __destruct() {
        $this->close();
    }
    
    /**
     * 上传文件
     * @param string $file_name
     */
    public function upload($file_name) {
        if(!$this->status) {
            return false;
        }
        
        if(!file_exists($file_name)) {
            return false;
        }
        
        $file_info = fastdfs_storage_upload_by_filename($file_name, null, array(), null, $this->tracker, $this->storage);
        if(!$file_info) {
            return false;
        }
        
        $result = fastdfs_get_file_info($file_info['group_name'], $file_info['filename']);
        $result['group_name'] = $file_info['group_name'];
        $result['file_name'] = $file_info['filename'];
        return $result;
    }
    
    /**
     * 替换文件
     * @param string $group_name
     * @param string $file_name
     */
    public function replace($group_name, $file_name) {
        return false;
    }
    
    /**
     * 删除文件
     * @param string $group_name
     * @param string $file_name
     */
    public function remove($group_name, $file_name) {
        if(!$this->status) {
            return false;
        }
        
        return fastdfs_storage_delete_file($group_name, $file_name);
    }
    
    /**
     * 下载文件流
     * @param string $group_name
     * @param string $file_name
     */
    public function download_buff($group_name, $file_name) {
        if(!$this->status) {
            return false;
        }
        
        return fastdfs_storage_download_file_to_buff($group_name, $file_name);
    }
    
    /**
     * 下载文件
     * @param string $group_name
     * @param string $file_name
     * @param string $save_path_file
     */
    public function download_file($group_name, $file_name, $save_path_file) {
        if(!$this->status) {
            return false;
        }
        
        $bool = fastdfs_storage_download_file_to_file($group_name, $file_name, $save_path_file);
        return $bool;
    }
    
    /**
     * 生成完整路径(举例,细节参考实际配置)
     * @param string $group_name
     * @param string $file_name
     */
    public function make_url($group_name, $file_name) {
        if(empty($group_name) || empty($file_name)) {
            return '';
        }
        
        $group = intval(substr($group_name, strlen('group')));
        if($group < 3) {
            $group_id = $group + 1;
        } else {
            $group_id = '1' . $group;
        }
        
        $full_path = 'http://img' . $group_id . '.ipmay.com/group' . $group . '/' . $file_name;
        return $full_path;
    }
    
    /**
     * 当前状态
     */
    public function valid() {
        return $this->status;
    }
    
    /**
     * 关闭连接
     */
    public function close() {
        if($this->status) {
            fastdfs_disconnect_server($this->connect);
            $this->tracker = null;
            $this->connect = null;
            $this->storage = null;
            $this->status = false;
        }
    }
}
?>


3