PHP code example of huangdingbo / yii2-uploader
1. Go to this page and download the library: Download huangdingbo/yii2-uploader library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
huangdingbo / yii2-uploader example snippets
'uploader' => [
'class' => 'uploader\FileUploaderComponent',
'allowExt' => '*', //运行的文件扩展名, "*" || ["*"] "" ["png","jpg"], * 表示不限制
'basePath' => '@webroot/static/uploader', //上传文件的根路径
'temp_path' => '@webroot/static/temp', //临时文件的存放路径
'chunk_path' => '@webroot/static/chunks', //切片的临时存放路径
'web_path' => '@web/static/uploader', //文件上传后的访问路径
'db' => 'db', //数据库连接组件
'table' => 'sys_upload_files' //数据表
]
$fileInfo = \Yii::$app->request->post();
$fileName = ArrayHelper::getValue($fileInfo,'fileName');
$size = ArrayHelper::getValue($fileInfo,'size');
$type = ArrayHelper::getValue($fileInfo,'type');
$chunkNum = ArrayHelper::getValue($fileInfo,'chunkNum');
$chunk = ArrayHelper::getValue($fileInfo,'chunk');
$chunkSize = ArrayHelper::getValue($fileInfo,'chunkSize');
if(!$fileName || !$size || !$type || !$chunkNum || !$chunk || !$chunkSize || !$_FILES["file"]["tmp_name"]){
return ["ok" => false,"code" => 201,"msg" => "参数不完整"];
}
//实例化组件
$handler = \Yii::$app->uploader;
//设置参数
$handler->setFileName($fileName)->setSize($size)->setType($type)->setTotalChunk($chunkNum)->setCurrentChunk($chunk)->setChunkSize($chunkSize);
//执行
$result = $handler->execute();
if (is_array($result)){
return $result;
}else{
return $handler->getError()[0];
}
[
200 => ["code" => 200,"msg" => "上传成功"],
1000 => ["code" => 1000,"msg" => "文件名不能为空"],
1001 => ["code" => 1001,"msg" => "文件缺少扩展名"],
1002 => ["code" => 1002,"msg" => "不允许上传该类型文件"],
1003 => ["code" => 1003,"msg" => "文件大小为空"],
1004 => ["code" => 1004,"msg" => "分片大小不能为空"],
1005 => ["code" => 1005,"msg" => "分片信息不能为空"],
1006 => ["code" => 1006,"msg" => "当前分片大于了总分片数据,文件修改了?"],
1007 => ["code" => 1007,"msg" => "临时文件不存在"],
1008 => ["code" => 1008,"msg" => "未配置运行的文件扩展名"],
1009 => ["code" => 1009,"msg" => "allowExt、temp_path、hunk_path、basePath需定义"],
1010 => ["code" => 1010,"msg" => "存在同名文件"],
1111 => ["code" => 1111,"msg" => "服务器已经存在分片","index" => 0],
2222 => ["code" => 2222,"msg" => "切片上传完成"],
];