• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 4年前 (2021-12-20) 32次浏览 已收录 0个评论

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

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

一,安装php的fastdfs扩展

cd /home/gaodaima/FastDFS/php_client             #/home/gaodaima/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/gaodaima/FastDFS/conf/client.conf

network_timeout=60
base_path=/home/gaodaima/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/gaodaima/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;
}
}
}

?>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:FastDFS分布式文件客户端安装,以及fastdfsapi

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址