PHP使用FastDFS上傳下載文件

準(zhǔn)備事項(xiàng)

PHP FastDFS擴(kuò)展安裝

#進(jìn)入fastdfs安裝包目錄下php_client文件夾
cd /fastdfs/php_client
#編譯安裝fastdfs_client擴(kuò)展
phpize  #對(duì)應(yīng)要編譯版本的phpize
./configure --with-php-config=/user/local/php/bin/php-config  #php安裝目錄下的php-config
make && make install 
#修改php.ini文件添加fastdfs相關(guān)配置(php_client文件夾下fastdfs_client.ini內(nèi)容添加到php.ini中)
cat ./fastdfs_client.ini >> /etc/php.ini
#重啟php
service php-fpm restart 
#驗(yàn)證,查看是否有fastdfs_client擴(kuò)展
php -m 
#修改/etc/fdfs/client.conf
#tracker_server為保存文件的主機(jī)域名ip

PHP上傳文件(YII2框架示例)

  • controller
public function actionFdfs()
{
    $file = $_FILES ?? '';
    $tmp_name = $file['file']['tmp_name'] ?? '';
    $type = $file['file']['type'] ?? '';
    $name = $file['file']['name'] ?? '';

    $curlFile = new \CurlFile($tmp_name, $type, $name);
    $fileSuffix = Common::getSuffix($curlFile->getPostFilename());

    $ret['file'] = $file;
    $ret['fileId'] = FastdfsUtil::uploadToFastdfs($curlFile, $fileSuffix);
    return $this->asJson($ret);
}
  • Common相關(guān)方法
static function getSuffix($fileName)
{
    preg_match('/\.(\w+)?$/', $fileName, $matchs);
    return isset($matchs[1])?$matchs[1]:'';
}
  • FastdfsUtil
<?php

namespace app\library\common\fastdfs;

class FastdfsUtil {

    //上傳文件到fastdfs
    static 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;
    }
}

前端頁(yè)面demo(VUE上傳組件)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>HTML5 GetUserMedia Demo</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" >
    <!-- 引入組件庫(kù) -->
</head>
<body>

<div id="app">
    <el-upload
        class="upload-demo"
        drag
        action="http://test.local/test/fdfs"
        multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div>
        <div class="el-upload__tip" slot="tip">只能上傳jpg/png文件,且不超過500kb</div>
    </el-upload>
</div>


<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data: {
            message: 'Hello Vue!'
        }
    })
</script>
</body>
</html>

下載文件重命名

  • 需調(diào)整nginx相關(guān)配置,fdfs.conf配置如下
server {
    listen       8888;    ## 該端口為storage.conf中的http.server_port相同
    server_name  localhost;
    location ~/group[0-9]/ {
        if ($arg_attname ~ "^(.+)") {
            #設(shè)置下載
            add_header Content-Type application/x-download;
            #設(shè)置文件名
            add_header Content-Disposition "attachment;filename=$arg_attname";
        }
        ngx_fastdfs_module;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容