FastDFS分布式文件服务器安装,及配置,测试(2)

四、测试以及使用FastDFS

1、FastDFS之配置client

vim /etc/fdfs/client.conf

base_path=/home/yuqing/fastdfs 修改为: base_path=/home/fastdfs

tracker_server=192.168.209.121:22122 修改为: tracker_server=10.201.20.237:22122

##include http.conf  修改为: #include http.conf

2、测试上传文件

用法:Usage: fdfs_test <config_file> upload <local_filename> [FILE | BUFF | CALLBACK]

测试:

cd /home/wwwroot/

fdfs_test /etc/fdfs/client.conf  upload  FlexPaper.zip

FastDFS分布式文件服务器安装,及配置,测试

:8080/group1/M00/00/00/CskU7U-c9hio4fAeACCD1c0mEbg924_big.zip

启动tracker服务:

/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf

启动storager服务:

/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf

监控:

/usr/local/bin/fdfs_monitor /etc/fdfs/storage.conf

PHP与FastDFS简单示例:

FastDFS.php

<?php
 
class FDFS{
 
    public  function __construct(){
        $this->tracker = fastdfs_tracker_get_connection();
        $this->server = fastdfs_connect_server($this->tracker['ip_addr'], $this->tracker['port']);
        $this->storage = fastdfs_tracker_query_storage_store();
 
        $this->server = fastdfs_connect_server($this->storage['ip_addr'], $this->storage['port']);
        if (!$this->server){
            error_log("errno1: " . fastdfs_get_last_error_no() . ", error info: " . fastdfs_get_last_error_info());
            exit(1);
        }
 
        $this->storage['sock'] = $this->server['sock'];
 
    }
 
    public  function fdfs_upload($input_name){
        $file_tmp = $_FILES[$input_name]['tmp_name'];
        $real_name = $_FILES[$input_name]['name'];
        $file_name = dirname($file_tmp)."/".$real_name;
        //@copy($file_tmp, $file_name);
        @rename($file_tmp, $file_name);
 
        $file_info = fastdfs_storage_upload_by_filename($file_name, null, array(), null, $this->tracker, $this->storage);
        //print_r($file_info);die;
        if($file_info){
            $group_name = $file_info['group_name'];
            $remote_filename = $file_info['filename'];
 
            $i = fastdfs_get_file_info($group_name, $remote_filename);
            $storage_ip = $i['source_ip_addr'];
            //var_dump($file_info);
            return array($remote_filename, $group_name, $storage_ip, $real_name);
        }
        return false;
    }
 
    public function fdfs_down($group_name, $file_id){
        $file_content = fastdfs_storage_download_file_to_buff($group_name, $file_id);
        return $file_content;
    }
 
    public  function fdfs_del($group_name, $file_id){
        fastdfs_storage_delete_file($group_name, $file_id);
    }
}
 
?>

index.php

<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
define('S_ROOT',dirname(__FILE__).'/');
define('DOWNLOAD_URL','http://192.168.1.110:8080');
require_once(S_ROOT.'FastDFS.php');
$conn = mysql_connect('localhost','root','123456');
$link = mysql_select_db('fastdfs',$conn);
 
if(!empty($_FILES['upload1'])){
    $fdfs_obj = new FDFS();
    $res = $fdfs_obj->fdfs_upload("upload1");
    $sql = "INSERT INTO `fdfs` (`file_id`, `group_id`, `storage_ip`, `real_name`) VALUES ('".$res[0]."', '".$res[1]."', '".$res[2]."', '".$res[3]."')";
    echo $sql.'<hr>';
    $result = mysql_query($sql);var_dump($result);
 
}else{
 
    $sql = 'SELECT * FROM `fdfs`';
    $query = mysql_query($sql);
    while($row = mysql_fetch_assoc($query))
    {
        $data[] = $row;
    }
    if(!empty($data))
    {
        foreach($data as $v)
        {
             echo '<a href="'https://www.linuxidc.com/Linux/2012-09/.DOWNLOAD_URL.'/'.$v['group_id'].'/'.$v['file_id'].'">'.$v['real_name'].'</a><br/>';
 
        }
    }
 
}
 
?>
 
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" /><br />
<input type="file" /><br />
<input type="submit" />
</form>
</body>
</html>

sql:

--

-- 数据库: `fastdfs`

--

 

-- --------------------------------------------------------

 

--

-- 表的结构 `fdfs`

--

 

CREATE TABLE IF NOT EXISTS `fdfs` (

  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',

  `file_id` varchar(200) NOT NULL DEFAULT '' COMMENT '文件ID',

  `group_id` varchar(50) NOT NULL DEFAULT '' COMMENT '组名',

  `real_name` varchar(200) NOT NULL COMMENT '真名',

  `storage_ip` varchar(16) NOT NULL COMMENT 'ip',

  PRIMARY KEY (`id`),

  KEY `i_file_id` (`file_id`)

) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

也可以重新封装下fastdfs_client 方法


wget


tar zxvf fastdfs_client_php_v1.6.tar.gz


cd fastdfs_client_php_v1.6.tar.gz


vim FastdfsClient.php


 

<?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;
  }
}}

?>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/0113049f45138e584e01a212e2524f0b.html