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

张映 发表于 2010-02-09

分类目录: php, 服务器相关

标签:, , ,

要使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;
}
}
}

?>


转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/server/219.html