Create a name for GitHub[uploadfile](https://github.com/guanguans/uploadfile)
New warehouse, and clone to local.
$ git clone [email protected]:guanguans/uploadfile.git
$ cd uploadfile
Copy the code
Second, initialize the project, generatecomposer.json
file
Step 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]: File [guanguans <[email protected]>, n to skip]: guanguans <[email protected]> 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": "a generic file upload package ", "type": "library", "require": {" PHP ": } "> = 5.4", "the require - dev" : {" PHP ":" > = 5.4 "}, "license" : "MIT", "the authors" : [{" name ":" guanguans ", "email" : "[email protected]" } ], "minimum-stability": "dev" } Do you confirm generation [yes]? yesCopy the code
2.2 Procedure Description
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. Enter a project name space / / note < vendor > / < name > must conform to [a - z0-9 _. -] + / / a - z0-9 _. - + Package name (< vendor > / < name >) dell/htdocs: yourname/projectname // 2. []: This is a test // 3. Author [Guanguans <[email protected]>, n to skip]: // 4. Input the Minimum stable version, stable, RC, beta, alpha, dev Minimum Stability []: dev // 5. Input project Type, Package Type (e.g. library, project, metapackage, composer-plugin) []: library // 6. Enter License []: > Define your dependencies. // 7. Would you like to define your dependencies (require) interactively [yes]? // If dependencies are required, enter the dependencies to install: PHP // Enter the version constraint to require (or leave blank to use the latest version): >=5.4.0 // If more than one is required, repeat the above two steps // 8. If you need require-dev dependencies (require-dev) interactively [yes] {"name": "guanguans/uploadfile", "description": "a common file upload package ", "type": "library", "require": {" PHP ": } "> = 5.4", "the require - dev" : {" PHP ":" > = 5.4 "}, "license" : "MIT", "the authors" : [{" name ":" guanguans ", "email" : "[email protected]" } ], "minimum-stability": "dev" } // 9. Json Do you confirm generation [yes]? yesCopy the code
Add auto loading
Append to the composer. Json generated in the previous step
"autoload": {
"psr-4": {
"Guanguans\\": "src/"
}
}
Copy the code
4. Build the project
4.1 newuploadfile/src/UploadFile.php
├─UploadFile │ ├─ SRC │ ├─ Uploadfile.php │ ├─ ├─ ├─ ├─ download.jsonCopy the code
<?php
/**
* 通用文件上传类
* @author guanguans <[email protected]>
*/
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;
}
}
Copy the code
4.2 test
4.2.1 Executing the Command on the TERMINALcomposer install
, will generatevendor
Table of contents and other documents
yzm@Alert MINGW64 /i/phpstudy/WWW/uploadfile
$ composer install
Copy the code
4.2.2 newuploadfile/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; $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()); }Copy the code
- 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> Single file upload </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> UploadFile </label> <input type="file" name="uploadfile[]"> </div> <button type="submit" class="btn btn-primary">Submit</button> </form> </body> </html>Copy the code
Holdings of local browser to access uploadfile/test/UpploadFile HTML for testing
Five, the addREADME.md
,LICENSE
,.gitignore
The final structure of the project is as follows:My package GitHub address
├ ─ uploadfile extension covered directory │ ├ ─ SRC expansion pack code directory │ │ ├ ─ uploadfile. PHP │ ├ ─ test test directory │ │ ├ ─ uploadfile. HTML │ │ ├ ─ UpploadfileTest. PHP │ ├─ │ ├─ ├─ ├─ ├.mdCopy the code
6. Push to GitHub
Git add. Git commit -m 'init' git tag v1.0.0Copy the code
7. Submit packages on GitHub toPackagist
- First sign up for Packagist and log in (GitHub login is possible)
- Click the Summit button in the top navigation bar
- Enter the address of the previous package on GitHub in the input box, as follows:
https://github.com/guanguans/uploadfile
- Then click the Check button and Packagist will Check whether the code of the warehouse address conforms to the requirements of the Package of Composer. If the Check is normal, the Submit button will appear. Click the Submit button again. Our package is submitted to Packagist
Set the composer package to automatically update
When we update the GitHub repository, the packages on Packagist are not automatically updated. Now let’s set the automatic update
8.1 copyProfile API Token
8.2 Opening the GitHub Projectsetting
, the choice ofIntegrations & services
To addpackagist service
, click on theTest service
8.3 Verifying the automatic update
If you go to the Packagist home page and find that there is no red circled message, the automatic update Settings are successful.
Ix. Use in the project
I use Laravel as an example
composer create-project laravel/laravel
cd laravel
composer require guanguans/uploadfile
Copy the code
other
- The common upload class uploadfile.class.php in ThinkPHP is modified
- This article is the first blog, you can go to browse more articles.
(after)
This article is the original article, reproduced without contact with me, but please indicate from guanguan blog https://guanguans.cn