# [開發 Composer 包詳細步驟](https://segmentfault.com/a/1190000013947602)
* https://segmentfault.com/a/1190000013947602
開發一個 composer 通用文件上傳包,發布到 Packagist,并在 Laravel 中測試。

## 一、GitHub 創建一個名[uploadfile](https://github.com/guanguans/uploadfile)新倉庫,并克隆至本地。
~~~
$ git clone git@github.com:guanguans/uploadfile.git
$ cd uploadfile
~~~
## 二、初始化項目,生成`composer.json`文件
### 2.1 步驟
~~~
yzm@Alert MINGW64 /i/phpstudy/WWW/uploadfile
$ composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
Package name (<vendor>/<name>) [yzm/try-make-package]: guanguans/uploadfile
Description []: 一個通用文件上傳包
Author [guanguans <53222411@qq.com>, n to skip]: guanguans <yzmguanguan@gmail.com>
Minimum Stability []: dev
Package Type (e.g. library, project, metapackage, composer-plugin) []: l ibrary
License []: MIT
Define your dependencies.
Would you like to define your dependencies (require) interactively [yes] ? yes
Search for a package: php
Enter the version constraint to require (or leave blank to use the lates t version): >=5.4.0
Search for a package:
Would you like to define your dev dependencies (require-dev) interactive ly [yes]? yes
Search for a package: php
Enter the version constraint to require (or leave blank to use the lates t version): >=5.4.0
Search for a package:
{
"name": "guanguans/uploadfile",
"description": "一個通用文件上傳包",
"type": "library",
"require": {
"php": ">=5.4"
},
"require-dev": {
"php": ">=5.4"
},
"license": "MIT",
"authors": [
{
"name": "guanguans",
"email": "yzmguanguan@gmail.com"
}
],
"minimum-stability": "dev"
}
Do you confirm generation [yes]? yes
~~~
### 2.2 步驟解釋
~~~
yzm@Alert MINGW64 /i/phpstudy/WWW/uploadfile
$ composer init
Welcome to the Composer config generator
This command will guide you through creating your composer.json config.
// 1. 輸入項目命名空間
// 注意<vendor>/<name> 必須要符合 [a-z0-9_.-]+/[a-z0-9_.-]+
Package name (<vendor>/<name>) [dell/htdocs]: yourname/projectname
// 2. 項目描述
Description []: 這是一個測試
// 3. 輸入作者信息,可以直接回車
Author [guanguans <53222411@qq.com>, n to skip]:
// 4. 輸入最低穩定版本,stable, RC, beta, alpha, dev
Minimum Stability []: dev
// 5. 輸入項目類型,
Package Type (e.g. library, project, metapackage, composer-plugin) []: library
// 6. 輸入授權類型
License []:
> Define your dependencies.
// 7. 輸入依賴信息
Would you like to define your dependencies (require) interactively [yes]?
// 如果需要依賴,則輸入要安裝的依賴
Search for a package: php
// 輸入版本號
Enter the version constraint to require (or leave blank to use the latest version): >=5.4.0
// 如需多個,則重復以上兩個步驟
// 8. 是否需要require-dev,
Would you like to define your dev dependencies (require-dev) interactively [yes]?
// 操作同上
{
"name": "guanguans/uploadfile",
"description": "一個通用文件上傳包",
"type": "library",
"require": {
"php": ">=5.4"
},
"require-dev": {
"php": ">=5.4"
},
"license": "MIT",
"authors": [
{
"name": "guanguans",
"email": "yzmguanguan@gmail.com"
}
],
"minimum-stability": "dev"
}
// 9. 是否生成composer.json
Do you confirm generation [yes]? yes
~~~
## 三、添加自動加載
在上一步生成的`composer.json`中追加
~~~
"autoload": {
"psr-4": {
"Guanguans\\": "src/"
}
}
~~~
## 四、構建項目
### 4.1 新建`uploadfile/src/UploadFile.php`
~~~
├─uploadfile
│ ├─src
│ │ ├─UploadFile.php
│ └─composer.json
~~~
~~~
<?php
/**
* 通用文件上傳類
* @author guanguans <yzmguanguan@gmail.com>
*/
namespace Guanguans; // 注意命名空間與 composer.json 中的一致
class UploadFile
{
private $config = [
'maxSize' => -1, // 上傳文件的最大值
'supportMulti' => true, // 是否支持多文件上傳
'allowExts' => [], // 允許上傳的文件后綴 留空不作后綴檢查
'allowTypes' => [], // 允許上傳的文件類型 留空不做檢查
'thumb' => false, // 使用對上傳圖片進行縮略圖處理
'imageClassPath' => 'ORG.Util.Image', // 圖庫類包路徑
'thumbMaxWidth' => '',// 縮略圖最大寬度
'thumbMaxHeight' => '',// 縮略圖最大高度
'thumbPrefix' => 'thumb_',// 縮略圖前綴
'thumbSuffix' => '',
'thumbPath' => '',// 縮略圖保存路徑
'thumbFile' => '',// 縮略圖文件名
'thumbExt' => '',// 縮略圖擴展名
'thumbRemoveOrigin' => false,// 是否移除原圖
'thumbType' => 1, // 縮略圖生成方式 1 按設置大小截取 0 按原圖等比例縮略
'zipImages' => false,// 壓縮圖片文件上傳
'autoSub' => false,// 啟用子目錄保存文件
'subType' => 'hash',// 子目錄創建方式 可以使用hash date custom
'subDir' => '', // 子目錄名稱 subType為custom方式后有效
'dateFormat' => 'Ymd',
'hashLevel' => 1, // hash的目錄層次
'savePath' => '',// 上傳文件保存路徑
'autoCheck' => true, // 是否自動檢查附件
'uploadReplace' => false,// 存在同名是否覆蓋
'saveRule' => 'uniqid',// 上傳文件命名規則
'hashType' => 'md5_file',// 上傳文件Hash規則函數名
];
// 錯誤信息
private $error = '';
// 上傳成功的文件信息
private $uploadFileInfo ;
public function __get($name){
if(isset($this->config[$name])) {
return $this->config[$name];
}
return null;
}
public function __set($name,$value){
if(isset($this->config[$name])) {
$this->config[$name] = $value;
}
}
public function __isset($name){
return isset($this->config[$name]);
}
/**
* 架構函數
* @access public
* @param array $config 上傳參數
*/
public function __construct($config=[]) {
if(is_array($config)) {
$this->config = array_merge($this->config,$config);
}
}
/**
* 上傳一個文件
* @access public
* @param mixed $name 數據
* @param string $value 數據表名
* @return string
*/
private function save($file) {
$filename = $file['savepath'].$file['savename'];
if(!$this->uploadReplace && is_file($filename)) {
// 不覆蓋同名文件
$this->error = '文件已經存在!'.$filename;
return false;
}
// 如果是圖像文件 檢測文件格式
if( in_array(strtolower($file['extension']), ['gif','jpg','jpeg','bmp','png','swf'])) {
$info = getimagesize($file['tmp_name']);
if(false === $info || ('gif' == strtolower($file['extension']) && empty($info['bits']))){
$this->error = '非法圖像文件';
return false;
}
}
if(!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename,'utf-8','gbk'))) {
$this->error = '文件上傳保存錯誤!';
return false;
}
if($this->thumb && in_array(strtolower($file['extension']), ['gif','jpg','jpeg','bmp','png'])) {
$image = getimagesize($filename);
if(false !== $image) {
//是圖像文件生成縮略圖
$thumbWidth = explode(',',$this->thumbMaxWidth);
$thumbHeight = explode(',',$this->thumbMaxHeight);
$thumbPrefix = explode(',',$this->thumbPrefix);
$thumbSuffix = explode(',',$this->thumbSuffix);
$thumbFile = explode(',',$this->thumbFile);
$thumbPath = $this->thumbPath?$this->thumbPath:dirname($filename).'/';
$thumbExt = $this->thumbExt ? $this->thumbExt : $file['extension']; //自定義縮略圖擴展名
// 生成圖像縮略圖
import($this->imageClassPath);
for($i=0,$len=count($thumbWidth); $i<$len; $i++) {
if(!empty($thumbFile[$i])) {
$thumbname = $thumbFile[$i];
}else{
$prefix = isset($thumbPrefix[$i])?$thumbPrefix[$i]:$thumbPrefix[0];
$suffix = isset($thumbSuffix[$i])?$thumbSuffix[$i]:$thumbSuffix[0];
$thumbname = $prefix.basename($filename,'.'.$file['extension']).$suffix;
}
if(1 == $this->thumbType){
Image::thumb2($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true);
}else{
Image::thumb($filename,$thumbPath.$thumbname.'.'.$thumbExt,'',$thumbWidth[$i],$thumbHeight[$i],true);
}
}
if($this->thumbRemoveOrigin) {
// 生成縮略圖之后刪除原圖
unlink($filename);
}
}
}
if($this->zipImags) {
// TODO 對圖片壓縮包在線解壓
}
return true;
}
/**
* 上傳所有文件
* @access public
* @param string $savePath 上傳文件保存路徑
* @return string
*/
public function upload($savePath ='') {
//如果不指定保存文件名,則由系統默認
if(empty($savePath))
$savePath = $this->savePath;
// 檢查上傳目錄
if(!is_dir($savePath)) {
// 檢查目錄是否編碼后的
if(is_dir(base64_decode($savePath))) {
$savePath = base64_decode($savePath);
}else{
// 嘗試創建目錄
if(!mkdir($savePath)){
$this->error = '上傳目錄'.$savePath.'不存在';
return false;
}
}
}else {
if(!is_writeable($savePath)) {
$this->error = '上傳目錄'.$savePath.'不可寫';
return false;
}
}
$fileInfo = [];
$isUpload = false;
// 獲取上傳的文件信息
// 對$_FILES數組信息處理
$files = $this->dealFiles($_FILES);
foreach($files as $key => $file) {
//過濾無效的上傳
if(!empty($file['name'])) {
//登記上傳文件的擴展信息
if(!isset($file['key'])) $file['key'] = $key;
$file['extension'] = $this->getExt($file['name']);
$file['savepath'] = $savePath;
$file['savename'] = $this->getSaveName($file);
// 自動檢查附件
if($this->autoCheck) {
if(!$this->check($file))
return false;
}
//保存上傳文件
if(!$this->save($file)) return false;
if(function_exists($this->hashType)) {
$fun = $this->hashType;
$file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
}
//上傳成功后保存文件信息,供其他地方調用
unset($file['tmp_name'],$file['error']);
$fileInfo[] = $file;
$isUpload = true;
}
}
if($isUpload) {
$this->uploadFileInfo = $fileInfo;
return true;
}else {
$this->error = '沒有選擇上傳文件';
return false;
}
}
/**
* 上傳單個上傳字段中的文件 支持多附件
* @access public
* @param array $file 上傳文件信息
* @param string $savePath 上傳文件保存路徑
* @return string
*/
public function uploadOne($file,$savePath=''){
//如果不指定保存文件名,則由系統默認
if(empty($savePath))
$savePath = $this->savePath;
// 檢查上傳目錄
if(!is_dir($savePath)) {
// 嘗試創建目錄
if(!mkdir($savePath,0777,true)){
$this->error = '上傳目錄'.$savePath.'不存在';
return false;
}
}else {
if(!is_writeable($savePath)) {
$this->error = '上傳目錄'.$savePath.'不可寫';
return false;
}
}
//過濾無效的上傳
if(!empty($file['name'])) {
$fileArray = [];
if(is_array($file['name'])) {
$keys = array_keys($file);
$count = count($file['name']);
for ($i=0; $i<$count; $i++) {
foreach ($keys as $key)
$fileArray[$i][$key] = $file[$key][$i];
}
}else{
$fileArray[] = $file;
}
$info = [];
foreach ($fileArray as $key=>$file){
//登記上傳文件的擴展信息
$file['extension'] = $this->getExt($file['name']);
$file['savepath'] = $savePath;
$file['savename'] = $this->getSaveName($file);
// 自動檢查附件
if($this->autoCheck) {
if(!$this->check($file))
return false;
}
//保存上傳文件
if(!$this->save($file)) return false;
if(function_exists($this->hashType)) {
$fun = $this->hashType;
$file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
}
unset($file['tmp_name'],$file['error']);
$info[] = $file;
}
// 返回上傳的文件信息
return $info;
}else {
$this->error = '沒有選擇上傳文件';
return false;
}
}
/**
* 轉換上傳文件數組變量為正確的方式
* @access private
* @param array $files 上傳的文件變量
* @return array
*/
private function dealFiles($files) {
$fileArray = [];
$n = 0;
foreach ($files as $key=>$file){
if(is_array($file['name'])) {
$keys = array_keys($file);
$count = count($file['name']);
for ($i=0; $i<$count; $i++) {
$fileArray[$n]['key'] = $key;
foreach ($keys as $_key){
$fileArray[$n][$_key] = $file[$_key][$i];
}
$n++;
}
}else{
$fileArray[$key] = $file;
}
}
return $fileArray;
}
/**
* 獲取錯誤代碼信息
* @access public
* @param string $errorNo 錯誤號碼
* @return void
*/
protected function error($errorNo) {
switch($errorNo) {
case 1:
$this->error = '上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值';
break;
case 2:
$this->error = '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值';
break;
case 3:
$this->error = '文件只有部分被上傳';
break;
case 4:
$this->error = '沒有文件被上傳';
break;
case 6:
$this->error = '找不到臨時文件夾';
break;
case 7:
$this->error = '文件寫入失敗';
break;
default:
$this->error = '未知上傳錯誤!';
}
return ;
}
/**
* 根據上傳文件命名規則取得保存文件名
* @access private
* @param string $filename 數據
* @return string
*/
private function getSaveName($filename) {
$rule = $this->saveRule;
if(empty($rule)) {//沒有定義命名規則,則保持文件名不變
$saveName = $filename['name'];
}else {
if(function_exists($rule)) {
//使用函數生成一個唯一文件標識號
$saveName = $rule().".".$filename['extension'];
}else {
//使用給定的文件名作為標識號
$saveName = $rule.".".$filename['extension'];
}
}
if($this->autoSub) {
// 使用子目錄保存文件
$filename['savename'] = $saveName;
$saveName = $this->getSubName($filename).$saveName;
}
return $saveName;
}
/**
* 獲取子目錄的名稱
* @access private
* @param array $file 上傳的文件信息
* @return string
*/
private function getSubName($file) {
switch($this->subType) {
case 'custom':
$dir = $this->subDir;
break;
case 'date':
$dir = date($this->dateFormat,time()).'/';
break;
case 'hash':
default:
$name = md5($file['savename']);
$dir = '';
for($i=0;$i<$this->hashLevel;$i++) {
$dir .= $name{$i}.'/';
}
break;
}
if(!is_dir($file['savepath'].$dir)) {
mkdir($file['savepath'].$dir,0777,true);
}
return $dir;
}
/**
* 檢查上傳的文件
* @access private
* @param array $file 文件信息
* @return boolean
*/
private function check($file) {
if($file['error']!== 0) {
//文件上傳失敗
//捕獲錯誤代碼
$this->error($file['error']);
return false;
}
//文件上傳成功,進行自定義規則檢查
//檢查文件大小
if(!$this->checkSize($file['size'])) {
$this->error = '上傳文件大小不符!';
return false;
}
//檢查文件Mime類型
if(!$this->checkType($file['type'])) {
$this->error = '上傳文件MIME類型不允許!';
return false;
}
//檢查文件類型
if(!$this->checkExt($file['extension'])) {
$this->error ='上傳文件類型不允許';
return false;
}
//檢查是否合法上傳
if(!$this->checkUpload($file['tmp_name'])) {
$this->error = '非法上傳文件!';
return false;
}
return true;
}
// 自動轉換字符集 支持數組轉換
private function autoCharset($fContents, $from='gbk', $to='utf-8') {
$from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
$to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
//如果編碼相同或者非字符串標量則不轉換
return $fContents;
}
if (function_exists('mb_convert_encoding')) {
return mb_convert_encoding($fContents, $to, $from);
} elseif (function_exists('iconv')) {
return iconv($from, $to, $fContents);
} else {
return $fContents;
}
}
/**
* 檢查上傳的文件類型是否合法
* @access private
* @param string $type 數據
* @return boolean
*/
private function checkType($type) {
if(!empty($this->allowTypes))
return in_array(strtolower($type),$this->allowTypes);
return true;
}
/**
* 檢查上傳的文件后綴是否合法
* @access private
* @param string $ext 后綴名
* @return boolean
*/
private function checkExt($ext) {
if(!empty($this->allowExts))
return in_array(strtolower($ext),$this->allowExts,true);
return true;
}
/**
* 檢查文件大小是否合法
* @access private
* @param integer $size 數據
* @return boolean
*/
private function checkSize($size) {
return !($size > $this->maxSize) || (-1 == $this->maxSize);
}
/**
* 檢查文件是否非法提交
* @access private
* @param string $filename 文件名
* @return boolean
*/
private function checkUpload($filename) {
return is_uploaded_file($filename);
}
/**
* 取得上傳文件的后綴
* @access private
* @param string $filename 文件名
* @return boolean
*/
private function getExt($filename) {
$pathinfo = pathinfo($filename);
return $pathinfo['extension'];
}
/**
* 取得上傳文件的信息
* @access public
* @return array
*/
public function getUploadFileInfo() {
return $this->uploadFileInfo;
}
/**
* 取得最后一次錯誤信息
* @access public
* @return string
*/
public function getErrorMsg() {
return $this->error;
}
}
~~~
### 4.2 測試
#### 4.2.1 終端下執行`composer install`,這時會生成`vendor`目錄,及其他文件
~~~
yzm@Alert MINGW64 /i/phpstudy/WWW/uploadfile
$ composer install
~~~
#### 4.2.2 新建`uploadfile/test/UpploadFileTest.php`、`uploadfile/test/UpploadFile.html`
* UpploadFileTest.php
~~~
<?php
require_once '../vendor/autoload.php';
use Guanguans\UploadFile;
$upload = new UploadFile();
$upload->maxSize = 1*1024*1024; // 默認為-1,不限制上傳大小
$upload->savePath = './upload/'; // 上傳根目錄
$upload->saveRule = 'uniqid'; // 上傳文件的文件名保存規則
$upload->uploadReplace = true; // 如果存在同名文件是否進行覆蓋
$upload->autoSub = true; // 上傳子目錄開啟
$upload->subType = 'date'; // 上傳子目錄命名規則
$upload->allowExts = ['jpg', 'png']; // 允許類型
if ($upload->upload()) {
var_dump($upload->getUploadFileInfo());
} else {
var_dump($upload->getErrorMsg());
}
~~~
* UpploadFile.html
~~~
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>uploadfile test</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<form action="UpploadfileTest.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>單文件上傳</label>
<input type="file" name="uploadfile">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<hr>
<form action="UpploadfileTest.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>多文件上傳</label>
<input type="file" name="uploadfile[]">
<input type="file" name="uploadfile[]">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</body>
</html>
~~~
4.2.3 本地瀏覽器訪問`uploadfile/test/UpploadFile.html`進行測試

## 五、添加`README.md`、`LICENSE`、`.gitignore`等文件,項目最終結構如下:[我的包GitHub地址](https://github.com/guanguans/uploadfile)
~~~
├─uploadfile 擴展包根目錄
│ ├─src 擴展包代碼目錄
│ │ ├─UploadFile.php
│ ├─test 測試目錄
│ │ ├─uploadfile.html
│ │ ├─UpploadfileTest.php
│ ├─.gitignore
│ ├─composer.json
│ ├─LICENSE
│ └─README.md
~~~
## 六、推送到 GitHub
~~~
git add .
git commit -m 'init'
git tag v1.0.0 // 記住打一個版本號
git push origin master
git push v1.0.0
~~~
## 七、將 GitHub 上的包提交到[Packagist](https://packagist.org/)
1. 首先要在[Packagist](https://packagist.org/)上注冊賬號并登錄(可以用 GitHub 直接登錄)
2. 點擊頂部導航條中的 Summit 按鈕
3. 在輸入框中輸入 GitHub 上的剛才包地址,如:`https://github.com/guanguans/uploadfile`
4. 然后點擊 Check 按鈕 Packagist 會去檢測此倉庫地址的代碼是否符合 Composer 的 Package 包的要求
檢測正常的話,會出現 Submit 按鈕,再點擊一下 Submit 按鈕,我們的包就提交到 Packagist 上了


## 八、設置 composer 包自動更新
上面提交上的包提交的包,當我們更新 GitHub 倉庫時,Packagist 上面的的包并不會自動更新,現在我們來設置一下自動更新
### 8.1 復制[Profile](https://packagist.org/profile/)API Token

### 8.2 打開 GitHub 項目`setting`,選擇`Integrations & services`,添加`packagist service`,點擊`Test service`





### 8.3 驗證是否已經自動更新
移步 Packagist 包主頁,發現已經沒有了紅色的圈住的提示,說明設置自動更新成功。

## 九、項目中使用
我以 Laravel 中使用舉例
~~~
composer create-project laravel/laravel
cd laravel
composer require guanguans/uploadfile
~~~

## 其他
* 本文通用上傳類由 ThinkPHP 中[UploadFile.class.php](https://github.com/top-think/thinkphp/blob/3.0/ThinkPHP/Extend/Library/ORG/Net/UploadFile.class.php)修改
* 本文首發**[琯琯博客](https://guanguans.cn/)**,可前往瀏覽更多文章。