W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
從 PhalApi-Library 擴(kuò)展庫(kù)中下載獲取 UCloud 擴(kuò)展包,如使用:
git clone https://git.oschina.net/dogstar/PhalApi-Library.git
然后把 UCloud 目錄復(fù)制到 ./PhalApi/Library/ 下,即:
cp ./PhalApi-Library/UCloud/ ./PhalApi/Library/ -R
到處安裝完畢!接下是插件的配置。
我們需要在 ./Config/app.php 配置文件中追加以下配置:
/** * 云上傳引擎,支持local,oss,upyun */ 'UCloudEngine' => 'local', /** * 本地存儲(chǔ)相關(guān)配置(UCloudEngine為local時(shí)的配置) */ 'UCloud' => array( //對(duì)應(yīng)的文件路徑 'host' => 'http://localhost/PhalApi/Public/upload' ),
對(duì)應(yīng)的文件路徑也可以獨(dú)立綁定一個(gè)二級(jí)域名,然后綁定至upload目錄,調(diào)用的圖片將顯示為http://image.xxx.com/項(xiàng)目名/文件名.jpg
再提一句,可能有些同學(xué)搞不懂對(duì)應(yīng)文件路徑,這里的upload路徑會(huì)在Public中自動(dòng)生成
/** * 云上傳引擎,支持local,oss,upyun */ 'UCloudEngine' => 'oss', /** * 云上傳對(duì)應(yīng)引擎相關(guān)配置 * 如果UCloudEngine不為local,則需要按以下配置 */ 'UCloud' => array( //上傳的API地址,不帶http://,以下api為阿里云OSS杭州節(jié)點(diǎn) 'api' => 'oss-cn-hangzhou.aliyuncs.com', //統(tǒng)一的key 'accessKey' => '', 'secretKey' => '', //自定義配置的空間 'bucket' => '', 'host' => 'http://image.xxx.com', //必帶http:// 末尾不帶/ 'timeout' => 90 ),
$loader->addDirs('Library'); //其他代碼... //云存儲(chǔ) DI()->ucloud = new UCloud_Lite();
先簡(jiǎn)單寫個(gè)測(cè)試文件:
<html> <form method="POST" action="./?service=Upload.upload" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit"> </form> </html>
<?php /* * +---------------------------------------------------------------------- * | 上傳接口 * +---------------------------------------------------------------------- * | Copyright (c) 2015 summer All rights reserved. * +---------------------------------------------------------------------- * | Author: summer <aer_c@qq.com> <qq7579476> * +---------------------------------------------------------------------- * | This is not a free software, unauthorized no use and dissemination. * +---------------------------------------------------------------------- * | Date * +---------------------------------------------------------------------- */ class Api_Upload extends PhalApi_Api { /** * 獲取參數(shù) * @return array 參數(shù)信息 */ public function getRules() { return array( 'upload' => array( 'file' => array( 'name' => 'file', 'type' => 'file', 'min' => 0, 'max' => 1024 * 1024, 'range' => array('image/jpg', 'image/jpeg', 'image/png'), 'ext' => array('jpg', 'jpeg', 'png') ), ), ); } /** * 上傳文件 * @return string $url 絕對(duì)路徑 * @return string $file 相對(duì)路徑,用于保存至數(shù)據(jù)庫(kù),按項(xiàng)目情況自己決定吧 */ public function upload() { //設(shè)置上傳路徑 設(shè)置方法參考3.2 DI()->ucloud->set('save_path',date('Y/m/d')); //新增修改文件名設(shè)置上傳的文件名稱 DI()->ucloud->set('file_name', 'avatar'); //上傳表單名 $res = DI()->ucloud->upfile($this->file); return $rs; } } ?>
按照以上設(shè)置,將會(huì)自動(dòng)生成4層目錄(demo/2015/13/7/aaa.jpg),demo其實(shí)為項(xiàng)目名稱,你可以在每個(gè)項(xiàng)目入口設(shè)置一個(gè)常量等于項(xiàng)目名稱,然后打開拓展library/UCloud/Lite.php找到$default_path,將該值設(shè)置為你設(shè)定的常量,或者為空(不是NULL),為空后你可以在設(shè)置上傳路徑里面設(shè)置(項(xiàng)目名/2015/12/07)也是可以的!
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: