php fastdfs上传文件,fastDFS中使用php上傳文件 -- http上傳與下載圖片

CleverCode研究完fastDFS后,嘗試着使用php上傳文件到fastdfs。

1 fastDFS安裝

fastdfs分布式架構配置參考:http://blog.csdn.net/clevercode/article/details/52267080。

fastdfs使用nginx配置參考:http://blog.csdn.net/clevercode/article/details/52276169。

fastdfs縮略圖生成參考:http://blog.csdn.net/clevercode/article/details/52278482。

2 fastDFS中php擴展的安裝

2.1 安裝

# cd /usr/local/src/fastdfs/FastDFS/php_client

# /usr/local/php5/bin/phpize

# ./configure --with-php-config=/usr/local/php5/bin/php-config

# make && make install

# cat fastdfs_client.ini >> /usr/local/php5/etc/php.ini

2.2 查看是否安裝成功

# php -m | grep fastdfs_client

b1ffd066f2bfb80bac56d21f146d5d14.png

2.3 配置fastDFS的client.conf

# vi /etc/fdfs/client.conf

tracker_server=192.168.101.135:22122

http.tracker_server_port=80

2.4重啟pkill php-fpm

# pkill php-fpm

# /usr/local/php5/sbin/php-fpm

3 通過http上傳

3.1 上傳頁面代碼

test.php

Filename:

3.2 接收文件php

upload.php

//上傳附件

function uploadAttach()

{/*{{{*/

$ret = array();

$ret['errorcode'] = 0;

$ret['errormsg'] = '';

if(!$_FILES || false == isset($_FILES["upFile"]))

{

$ret['errorcode'] = 1;

$ret['errormsg'] = "ERROR:upFile is not set";

return $ret;

}

$file = $_FILES["upFile"];

if (false == isset($file['tmp_name']) || false == is_file($file['tmp_name']))

{

$ret['errorcode'] = 2;

$ret['errormsg'] = "tmp_name is not file";

return $ret;

}

if (0 == filesize($file['tmp_name']))

{

$ret['errorcode'] = 3;

$ret['errormsg'] = "tmp_name filesize is 0";

return $ret;

}

$curlFile = new CurlFile($file['tmp_name'], $file['type'], $file['name']);

$fileSuffix = getSuffix($curlFile->getPostFilename());

$ret['file'] = $file;

$ret['fileId'] = uploadToFastdfs($curlFile, $fileSuffix);

return $ret;

}/*}}}*/

//獲取后綴

function getSuffix($fileName)

{/*{{{*/

preg_match('/\.(\w+)?$/', $fileName, $matchs);

return isset($matchs[1])?$matchs[1]:'';

}/*}}}*/

//上傳文件到fastdfs

function uploadToFastdfs(CurlFile $file, $fileSuffix)

{/*{{{*/

$fdfs = new FastDFS();

$tracker = $fdfs->tracker_get_connection();

$fileId = $fdfs->storage_upload_by_filebuff1(file_get_contents($file->getFilename()), $fileSuffix);

$fdfs->tracker_close_all_connections();

return $fileId;

}/*}}}*/

function start()

{

$ret = uploadAttach();

print_r($ret);

}

start();

?>

3.3 上傳一張圖片

48bbd698abe03715b172408c765d2aea.png

3.4 上傳結果

778fe9ead8cf764cea70384afb0b28b3.png

3.5 訪問(下載) http://192.168.101.132/group1/M00/00/00/wKhlhVfBiu2AWrzoAAKp3t_hiGI748.png

d5536a6ce935b66f7dbaa28eb16997eb.png

4 curl上傳

3 的http上傳方式,需要在每一台php服務器上都按裝fastdfs的php擴展。這里通過curl方式,直接上傳到具有fastdfs擴展的php服務器上。

curlupload.php

function curl_multipart_post($url, $post_data = array(), $file_fields = array(), $timeout=30)

{/*{{{*/

$result = array('errno' => 0, 'errmsg' => '', 'result' => '');

$ch = curl_init();

//set various curl options first

// set url to post to

curl_setopt($ch, CURLOPT_URL, $url);

// return into a variable rather than displaying it

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//set curl function timeout to $timeout

curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_VERBOSE, false);

//set method to post

curl_setopt($ch, CURLOPT_POST, true);

// disable Expect header

// hack to make it working

$headers = array("Expect: ");

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// initialize result post array

$result_post = array();

//generate post string

$post_array = array();

$post_strings = array();

if (!is_array($post_data)) {

$result['errno'] = 5;

$result['errmsg'] = 'Params error.';

return json_encode($result);

// return false;

}

foreach($post_data as $key=>$value) {

$post_array[$key] = $value;

$post_strings[] = urlencode($key)."=".urlencode($value);

}

$post_string = implode("&", $post_strings);

// set post string

// curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);

// set multipart form data - file array field-value pairs

if (!empty($file_fields)) {

foreach($file_fields as $key => $value) {

if (strpos(PHP_OS, "WIN") !== false) {

$value = str_replace("/", "\\", $value); // win hack

}

$file_fields[$key] = "@".$value;

}

}

// set post data

$result_post = array_merge($post_array, $file_fields);

curl_setopt($ch, CURLOPT_POSTFIELDS, $result_post);

// print_r($result_post);

//and finally send curl request

$output = curl_exec($ch);

$result['result'] = $output;

// print_r($result);

if (curl_errno($ch )) {

$result['errno'] = curl_errno($ch);

$result['errmsg'] = curl_error($ch);

// return false;

} else {

// return $result;

}

curl_close($ch);

return $result;

}/*}}}*/

function start()

{

$url = 'http://192.168.101.132/upload.php';

$post = array('signature' => 123456);

$fileFields = array('upFile' => '/data0/webRoot/upload.php');

$ret = curl_multipart_post($url,$post,$fileFields);

print_r($ret);

}

start();

?>

打印結果

de221abbe9170a05e6e4ef494cf2fba1.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值