php 中FastDFS開啟和呼叫使用
php 中FastDFS開啟和呼叫使用
1 <?php
2
3 if (!class_exists('FastDFS', false)) {
4 define('FDFS_PROTO_PKG_LEN_SIZE', 8);
5 define('FDFS_PROTO_CMD_ACTIVE_TEST', 111);
6 define('FDFS_PROTO_CMD_RESP', 100);
7 define('FDFS_PROTO_CMD_UPLOAD_SLAVE_FILE', 21);
8 define ('FDFS_PROTO_CMD_DELETE_FILE', 12);
9 define('FDFS_PROTO_CMD_GET_METADATA', 15);
10 define('FDFS_PROTO_CMD_SET_METADATA', 13);
11
12 //body_length + command + status
13 define('FDFS_HEADER_LENGTH', 10);
14 define('FDFS_IP_ADDRESS_SIZE', 16);
15 define('FDFS_FILE_EXT_NAME_MAX_LEN' , 6);
16 define('FDFS_GROUP_NAME_MAX_LEN', 16);
17 define('FDFS_OVERWRITE_METADATA', 1);
18 define('FDFS_MERGE_METADATA', 2);
19
20 // 連線超時時間
21 define('FDFS_CONNECT_TIME_OUT', 5);
22 define('FDFS_FILE_PREFIX_MAX_LEN', 16);
23
24 //傳輸超時時間
25 define('FDFS_TRANSFER_TIME_OUT' , 0);
26 define('FDFS_QUERY_STORE_WITHOUT_GROUP_ONE', 101);
27 define('FDFS_QUERY_STORE_WITH_GROUP_ONE', 104);
28
29 define('FDFS_TRACKER_QUERY_STORAGE_STORE_BODY_LEN', FDFS_GROUP_NAME_MAX_LEN + FDFS_IP_ADDRESS_SIZE + FDFS_PROTO_PKG_LEN_SIZE);
30
31 class FastDFS {
32
33 public $gConfig = array();
34
35 /**
36 *
37 * @var FastDFSTrackerClient
38 */
39 private $tracker;
40
41 /**
42 *
43 * @var FastDFSStorageClient
44 */
45 private $storage;
46 private $error = array(
47 'code' => 0,
48 'msg' => ''
49 );
50
51 /**
52 * 要使用這個類,你一定要在php的ini檔案中進行fastdfs的配置
53 *
54 * @throws FastDFSException
55 */
56 public function __construct() {
57 $configFile = '';
58 $ini = parse_ini_file(php_ini_loaded_file());
59
60 if (!isset($ini['fastdfs_client.tracker_group_count'])) {
61 throw new FastDFSException("no define fastdfs config");
62 }
63 for ($i = 0; $i < $ini['fastdfs_client.tracker_group_count']; $i++) {
64 if (isset($ini['fastdfs_client.tracker_group' . $i])) {
65 $configFile = $ini['fastdfs_client.tracker_group' . $i];
66 break;
67 }
68 }
69 if (!file_exists($configFile)) {
70 throw new FastDFSException("client config $configFile not found");
71 }
72 $this->gConfig = parse_ini_file($configFile);
73 list($this->gConfig['tracker_host'], $this->gConfig['tracker_port']) = explode(':', $this->gConfig['tracker_server']);
74 }
75
76 /**
77 * 獲得一個tracker
78 *
79 * @return \FastDFSTrackerClient
80 */
81 public function tracker_get_connection() {
82 $this->tracker = new FastDFSTrackerClient($this, $this->gConfig['tracker_host'], $this->gConfig['tracker_port']);
83
84 return $this->tracker;
85 }
86
87 /**
88 * 通過tracker獲取一個stroage
89 *
90 * @param string $groupName 檔案組名,當為空時,組名由tracker決定
91 * @param FastDFSTrackerClient $tracker
92 * @return \FastDFSStorageClient
93 */
94 public function tracker_query_storage_store($groupName, FastDFSTrackerClient $tracker) {
95 $this->storage = new FastDFSStorageClient($this, $groupName, $tracker);
96
97 return $this->storage;
98 }
99
100 /**
101 * 測試一下tracker伺服器是否正常
102 *
103 * @param FastDFSTrackerClient $tracker
104 * @return boolean
105 */
106 public function active_test(FastDFSTrackerClient $tracker = null) {
107 $this->initTrackerAndStorage($tracker);
108
109 $header = self::packHeader(FDFS_PROTO_CMD_ACTIVE_TEST, 0);
110 $tracker->send($header);
111
112 $resHeader = self::parseHeader($tracker->read(FDFS_HEADER_LENGTH));
113
114 return $resHeader['status'] == 0 ? true : false;
115 }
116
117 public function get_last_error_no() {
118 return $this->error['code'];
119 }
120
121 public function add_error($errorNo, $info) {
122 $this->error['code'] = $errorNo;
123 $this->error['msg'] = $info;
124 }
125
126 public function get_last_error_info() {
127 return $this->error['msg'];
128 }
129
130 /**
131 * 在storage中刪除一個檔案
132 *
133 * @param string $groupName 檔案所在的組名
134 * @param string $remoteFile 要刪除的檔案路徑
135 * @param FastDFSStorageClient $tracker
136 * @param FastDFSStorageClient $storage
137 */
138 public function storage_delete_file($groupName, $remoteFile, FastDFSStorageClient $tracker, FastDFSStorageClient $storage) {
139 $this->initTrackerAndStorage($tracker, $storage, $groupName);
140
141 $this->storage->deleteFile($groupName, $remoteFile);
142 }
143
144 /**
145 * 往storage中上傳一個檔案
146 *
147 * @param string $localFile 你本地的檔案路徑
148 * @param string $extName 檔案的副檔名,當名優提供副檔名時,會自動取檔案的副檔名
149 * @param array $metas 檔案的附加資訊
150 * @param string $groupName 所在的組名,可以為空,為空時,由tracker決定
151 * @param FastDFSTrackerClient $tracker
152 * @param FastDFSStorageClient $storage
153 */
154 public function storage_upload_by_filename($localFile, $extName = '', $metas = array(), $groupName = '', FastDFSTrackerClient $tracker = null, FastDFSStorageClient $storage = null) {
155 $this->initTrackerAndStorage($tracker, $storage, $groupName);
156
157 return $this->storage->uploadByFilename($localFile, $extName, $metas);
158 }
159
160 /**
161 * 上傳一個檔案的附屬檔案,主要使用一個圖片有縮圖的情況下
162 *
163 * @param string $localFile 本地檔案的路徑,縮圖的檔案路徑
164 * @param string $groupName 組名,最好和主檔案在同一個組
165 * @param string $masterFileName 主檔名
166 * @param string $prefix 檔案的字首
167 * @param string $extName 檔案的字尾,可以為空,為空時,由tracker決定
168 * @param array $meta 附件資訊
169 * @param FastDFSTrackerClient $tracker
170 * @param FastDFSStorageClient $storage
171 */
172 public function storage_upload_slave_by_filename($localFile, $groupName, $masterFileName, $prefix = '', $extName = '', $meta = array(), FastDFSTrackerClient $tracker = null, FastDFSStorageClient $storage = null) {
173 $this->initTrackerAndStorage($tracker, $storage, $groupName);
174 /*echo $localFile."<br/>".$groupName."<br/>".$masterFileName."<br/>".$prefix;
175 exit;*/
176 return $this->storage->uploadSalveFile($localFile, $groupName, $masterFileName, $prefix, $extName, $meta);
177 }
178
179 /**
180 * 檢查這個檔案是否已經存在
181 *
182 * @param string $groupName 檔案所在組名
183 * @param string $remoteFile 檔案在storage中的名字
184 * @param FastDFSStorageClient $tracker
185 * @param FastDFSStorageClient $storage
186 */
187 public function storage_file_exist($groupName, $remoteFile, FastDFSTrackerClient $tracker, FastDFSStorageClient $storage) {
188 $this->initTrackerAndStorage($tracker, $storage, $groupName);
189
190 return $this->storage->fileExists($groupName, $remoteFile);
191 }
192
193 public function close() {
194 if ($this->tracker) {
195 $this->tracker->close();
196 $this->tracker = null;
197 }
198 }
199
200 public function tracker_close_all_connections() {
201 $this->close();
202 if (!$this->storage) {
203 $this->storage->close();
204 }
205 }
206
207 public static function padding($str, $len) {
208
209 $str_len = strlen($str);
210
211 return $str_len > $len ? substr($str, 0, $len) : $str . pack('x' . ($len - $str_len));
212 }
213
214 /**
215 *
216 * @param int $command
217 * @param int $length
218 * @return bytes
219 */
220 public static function packHeader($command, $length = 0) {
221 return self::packU64($length) . pack('Cx', $command);
222 }
223
224 public static function packMetaData($data) {
225 $S1 = "\x01";
226 $S2 = "\x02";
227
228 $list = array();
229 foreach ($data as $key => $val) {
230 $list[] = $key . $S2 . $val;
231 };
232
233 return implode($S1, $list);
234 }
235
236 public static function parseMetaData($data) {
237
238 $S1 = "\x01";
239 $S2 = "\x02";
240
241 $arr = explode($S1, $data);
242 $result = array();
243
244 foreach ($arr as $val) {
245 list($k, $v) = explode($S2, $val);
246 $result[$k] = $v;
247 }
248
249 return $result;
250 }
251
252 public static function parseHeader($str, $len = FDFS_HEADER_LENGTH) {
253
254 assert(strlen($str) === $len);
255
256 $result = unpack('C10', $str);
257
258 $length = self::unpackU64(substr($str, 0, 8));
259 $command = $result[9];
260 $status = $result[10];
261
262 return array(
263 'length' => $length,
264 'command' => $command,
265 'status' => $status
266 );
267 }
268
269 /**
270 * From: sphinxapi.php
271 */
272 private static function unpackU64($v) {
273 list ( $hi, $lo ) = array_values(unpack("N*N*", $v));
274
275 if (PHP_INT_SIZE >= 8) {
276 if ($hi <
相關推薦
php 中FastDFS開啟和呼叫使用
php 中FastDFS開啟和呼叫使用
1 <?php
2
3 if (!class_exists('FastDFS', false)) {
4 define('FDFS_PROTO_PKG_LEN_SIZE', 8);
5
PHP中new static() 和 new self() 的區別
pub 堆內存 func sel urn ret 通過 ati php self 指的是self所在的類
new static 實例化的是當前使用的類,有點像$this ,從堆內存中提取出來。
還是通過實例說明一下:
class A { public static f
php中的isset()和empty()
methods 不存在 php 手冊 代碼 code ram post strong isset()和empty()的功能很類似,只能說是很類似,因為他們還是有不同點的,而他們的不同點還是挺明顯的;
官方手冊中對這兩個的解釋分別如下:
bool empty ( mixed
PHP中的__call和__callStatic方法(未看完)
def 不可見 重載方法 varchar baidu value dso argument dom 如何防止調用不存在的方法而出錯,使用__call魔術重載方法.
__call方法原型如下:
mixed __call(string $name,array $argume
2018/04/21 PHP 中的Session和Cookie知識總結
結束 方式 全局變量 過期 ESS 簡單 star 如何 lencod 在之後的工作和學習中,更是發現了自己對於基礎知識的不紮實,於是又返回頭來學習,確實很多東西是之前沒註意或是沒掌握的。
著重講一下這幾個問題
--
什麽是 Cookie ?
簡單來說:
因為 HTTP
php中靜態方法和靜態屬性的介紹
靜態屬性 size col 實例 生效 訪問類 都是 靜態 self 靜態分為兩個部分:靜態屬性和靜態方法
靜態的東西都是給類用的(包括類常量),非靜態的都是給對象用的
靜態屬性
在定義屬性的時候,使用關鍵字static修飾的屬性稱之為靜態屬性。
靜態方法
使用sta
php學習筆記:第四節--php中的運算子和流程控制
PHP的運算子、流程控制和其他語言的運算子一樣,沒什麼特別的,這裡不再累贅。
特別的運算子:
.= $a.=$b
PHP 中 call_user_func 函式 和 call_user_func_array 函式
PHP 中 call_user_func() 函式 和 call_user_func_array()函式都是回撥函式
區別:
call_user_func() 可以有多個引數,第一個引數為被呼叫的回撥函式,除了第一個引數外,其他引數均為被呼叫函式的引數
c
php中的 nl2br() 和 strpos() 函式
nl2br()函式
我們在換行時使用的一般都是<br>,但是在大段需要換行的程式碼時,使用<br>就有些繁瑣了,這時就可以使用nl2br()函數了
nl2br()函式是在所以新行之前插入HTML換行標記,它的語法為
string nl2br(string
PHP中file_put_contents追加和換行
在PHP的一些應用中需要寫日誌或者記錄一些資訊,這樣的話。可以使用fopen(),fwrite()以及 fclose()這些進行操作。也可以簡單的使用file_get_contents()和file_put_contents().
file_put_contents()寫檔案。預設的是重新寫檔案
【宇潤日常瘋測-005】PHP 中的 clone 和 new 效能比較
clone和new本不應該放在一起比較,它們的作用是不同的。但可能有一些場景下,可以用clone也可以用new,那麼這時候我們選哪個呢?
我編寫了兩個測試,第一個是宣告一個空類,第二個是帶構造方法、屬性的類。另外,我還加入了 PHP 序列化的測試。
國際慣例,直接上程式碼,一目瞭然。
程式碼
<?php
詳解PHP中的堆和棧
在PHP中共有8種資料型別,其中4中標量型別(字串、布林型、整型、浮點型)、2種複合型別(物件、陣列)、2種特殊型別(資源、NULL)。他們在執行的時候都要載入到記憶體中去用,那麼在記憶體裡面它們是怎麼表示的呢?
初始化靜態常量段:通常是指用來存放程式中已初始化且不為0的全域性變數如:靜態變數和
PHP中new self()和new static()的區別--延遲靜態載入
1.new static()是在PHP5.3版本中引入的新特性。
2.無論是new static()還是new self(),都是new了一個新的物件。
3.這兩個方法new出來的物件有什麼區別呢,說白了就是new出來的到底是同一個類例項還是不同的類例項呢?
為了探究上面的問題,我們
PHP中的Cookie和Session
一、php中的cookie
關於php中的cookie涉及到三個操作:設定cookie、查詢cookie、刪除cookie
1.1 設定cookie,必須在<html>標籤之前;
1.2 查詢cookie:找到某個cookie、展示所有的cookie
php中的加入和去除反斜槓
php中要給指定的指定的字元加上或去除反斜槓,可使用現成的函式:addcslashes() & stripcslashes() 來實現,例項程式碼:
<?php
$str="select * from tables where n..
【PHP】解析PHP中的錯誤和異常處理
not tty 不支持 版本 adding all 操作 lin ttr 目錄結構:
contents structure [-]
錯誤級別
自定義處理器
設置異常日誌
自定義異常類
在這篇文章中,筆者將會闡述PHP中的異常處理,希望能夠對你
php中的isset和empty,以及echo print和print_r的區別
先說下isset和empty的區別:
isset是判斷這個變數存在不存在或者如果存在的話是不是NULL。也就是說如果一個變數不存在或者變數值為NULL,那麼都認為是沒有isset的,也就是是isset得到的值為false。
empty不僅判斷這個變數存在不存在或者是否為NU
php中json資料和xml資料的封裝
<?php
/*
* 作用:將p轉換為josn格式的資料
* 備註:json_encode只接受UTF-8的編碼資料
*/
//echo json_encode($arr);
/*
* 作用:將p3資料從p1編碼轉換為p2編碼
* p1: 原編碼
* p
php中的explode和implode
explode的功能是使用一個字串分割另一個字串。返回一個分割後的陣列。
來看explode的基本用法:
<?php
$str = 'xaaxbbx';
$data = explode('x', $str);
echo "<pre>";
print_r
PHP中isset(變數)和直接判斷變數的區別
在變數為空字串和布林值false以及數值0時,isset全部返回true,直接判斷全部返回false:
$empty='';
echo (isset($empty)?'1':0);//1
echo '<br/>';
echo ($empty?'