• 云途科技成立于2010年 - 专注全球跨境电商服务器租赁托管!
  • 帮助中心

    您可以通过下方搜索框快速查找您想知道的问题

    FastDFS分布式文件客户端安装,以及fastdfsapi

      in  unix      Tags: 

    要使php程序利用fastdfs上传文件,要做到以下几点

    wget http://fastdfs.googlecode.com/files/fastdfs_client_php_v1.6.tar.gz

    一,安装php的fastdfs扩展

    cd /home/zhangy/FastDFS/php_client             #/home/zhangy/FastDFS  这个目录是fastdfs服务器源,解压目录
    /usr/local/php/bin/phpize
    ./configure --with-php-config=/usr/local/php/bin/php-config
    make && make install

    二,配置fastdfs客户端,以及php.ini

    vi /home/zhangy/FastDFS/conf/client.conf

    network_timeout=60
    base_path=/home/zhangy/FastDFS

    tracker_server=192.168.1.17:22122

    #standard log level as syslog, case insensitive, value list:
    ### emerg for emergency
    ### alert
    ### crit for critical
    ### error
    ### warn for warning
    ### notice
    ### info
    ### debug
    log_level=info

    #HTTP settings
    http.tracker_server_port=8080

    #use "#include" directive to include HTTP other settiongs
    ##include http.conf

    vi /usr/local/php/lib/php.ini

    在文件最后加上以下内容

    extension = fastdfs_client.so
    fastdfs_client.tracker_group_count = 1
    fastdfs_client.tracker_group0 = /home/zhangy/FastDFS/conf/client.conf

    重起apache或者Nginx或者支持你php的东西

    三,php fastdfsapi解压

    tar zxvf fastdfs_client_php_v1.6.tar.gz

    解压里面的东西,就是php版的api了.代码比较乱,如果在外面在套一层,在封装一下就挺完美的了,纯属个人观点。哈哈。

    四,我一个同事又将它封装了 一下,在这儿借花献佛

    <?php
    
    require_once 'fdfs_common.php';
    require_once 'fdfs_tracker_client.php';
    require_once 'fdfs_storage_client1.php';
    
    class FastdfsClient {
    private $_tracker_server = null;
    
    public function __construct() {
    $this->_getTracker();
    }
    
    public function __destruct() {
    fdfs_quit($this->_tracker_server);
    tracker_close_all_connections();
    }
    
    public function getInstance() {
    static $instance;
    if (!isset($instance)) {
    $class = __CLASS__;
    $instance = new $class();
    }
    return $instance;
    }
    
    /**
    * Upload file by filename to Fastdfs
    *
    * @param $local_filename local file name to upload
    * @param $meta_list metadata assoc array (key value pair array)
    * @param $group_name you can specify the group to upload file to
    *
    * @return fileid compose of $group_name and $remote_name, false for error
    */
    public function saveFile($local_filename, $file_ext_name = '', $meta_list = array(), $group_name = '') {
    $this->_getTracker();
    $storage_server = null;
    $file_id = null;
    
    $result = storage_upload_by_filename1($this->_tracker_server, $storage_server,
    $local_filename, $meta_list, $file_id, $group_name, $file_ext_name);
    
    if ($result == 0) {
    $file_id = str_replace("M00/",IMAGESHOWURL,$file_id);
    return $file_id;
    } else {
    throw new Exception('fdfs_upload_by_filename_failed', $result);
    return false;
    }
    }
    
    /**
    * Upload file by buff to the Fastdfs
    *
    * @param $file_buff the file content to upload
    * @param $file_ext_name the file ext name (not including dot)
    * @param $meta_list metadata assoc array (key value pair array)
    * @param $group_name you can specify the group to upload file to
    *
    * @return fileid composed of $group_name and $remote_name, false for error
    */
    public function saveFilebuff($file_buff, $file_ext_name, $meta_list = array(), $group_name = '') {
    $storage_server = null;
    $file_id = null;
    
    $file_size = strlen($file_buff);
    $result = storage_upload_by_filebuff1($this->_tracker_server, $storage_server,
    $file_buff, $file_size, $file_ext_name, $meta_list, $file_id, $group_name);
    if ($result == 0) {
    return $file_id;
    } else {
    throw new Exception('fdfs_upload_by_filebuff_failed', $result);
    return false;
    }
    }
    
    /**
    * Copy file from Fastdfs to local
    *
    * @param $file_id
    * @param $local_filename
    * @return bool
    */
    public function copyFile($file_id, $local_filename) {
    $storage_server = null;
    $file_size = null;
    
    $result = storage_download_file_to_file1($this->_tracker_server, $storage_server,
    $file_id, $local_filename, $file_size);
    
    if ($result == 0) {
    return true;
    } else {
    throw new Exception('fdfs_copy_file_failed', $result);
    return false;
    }
    }
    
    /**
    * Get file content from Fastdfs
    *
    * @param $file_id
    * @return file content
    */
    public function getFilebuff($file_id) {
    $storage_server = null;
    $file_size = null;
    $file_buff = null;
    
    $result = storage_download_file_to_buff1($this->_tracker_server, $storage_server,
    $file_id, $file_buff, $file_size);
    if ($result == 0) {
    return $file_buff;
    } else {
    throw new Exception('fdfs_load_file_to_buff_failed', $result);
    return false;
    }
    }
    
    /**
    * Delete file by fileid from the Fastdfs
    *
    * @param $file_id
    * @return bool
    */
    public function deleteFile($file_id) {
    if (empty($file_id)) return false;
    if (is_array($file_id)) {
    foreach ($file_id as $fid) {
    $this->deleteFile($fid);
    }
    return true;
    }
    
    $storage_server = null;
    $result = storage_delete_file1($this->_tracker_server, $storage_server, $file_id);
    
    if ($result == 0) {
    return true;
    } else {
    return false;
    }
    }
    
    /**
    * Get a tracker server
    */
    private function _getTracker() {
    $this->_tracker_server = null;
    $this->_tracker_server = tracker_get_connection();
    if ($this->_tracker_server == false) {
    throw new Exception('fdfs_get_tracker_server_failed');
    return false;
    }
    }
    }
    
    ?>
    


    • 外贸虚拟主机

      1GB硬盘

      2个独立站点

      1000M带宽

      不限制流量

      美国外贸专用虚拟主机,cPanel面板,每天远程备份.
      服务器配置:2*E5 32核,96GB 内存,4*2TB 硬盘 RAID10 阵列.

      ¥180/年

    • 美国/荷兰外贸VPS

      2核CPU

      1G内存

      30硬盘

      10M带宽

      美国/荷兰外贸云服务器,专注外贸服务器行业12年.
      服务器配置:2*E5 32核,96GB 内存,4*2TB 硬盘 RAID10 阵列.

      ¥99/月

    • 全球外贸服务器

      8核CPU

      32G内存

      1TB硬盘

      1000M带宽

      已部署数据中心:美国洛杉矶/亚特兰大、荷兰、加拿大、英国伦敦、德国、拉脱维亚、瑞典、爱沙尼亚
      自有机柜(全球九大数据中心),稳定在线率:99.9%

      ¥999/月 原价1380

    7*24小时 在线提交工单

    如果您的问题没有得到解决,推荐您在线提交工单,我们的客服人员会第一时间为您解决问题

    展开