Initial commit,修正
This commit is contained in:
Executable
+379
@@ -0,0 +1,379 @@
|
||||
<?php
|
||||
|
||||
# @Author: 嗨噜客(三亚) <fm453>
|
||||
# @Date: 2022-05-10T01:13:34+08:00
|
||||
# @Email: fm453@lukegzs.com
|
||||
# @Last modified by: fm453
|
||||
# @Last modified time: 2022-05-14T21:12:32+08:00
|
||||
# @Copyright: www.hiluker.cn
|
||||
|
||||
namespace backend\controllers;
|
||||
|
||||
use Yii;
|
||||
use yii\data\Pagination;
|
||||
use yii\web\UploadedFile;
|
||||
use common\models\CAttachment;
|
||||
use backend\controllers\Common;
|
||||
|
||||
class UploadController extends Common
|
||||
{
|
||||
public function beforeAction($action)
|
||||
{
|
||||
if (!$this->pid) {
|
||||
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
|
||||
}
|
||||
return parent::beforeAction($action);
|
||||
}
|
||||
|
||||
public function actionIndex()
|
||||
{
|
||||
$apis = [
|
||||
'image'=>'上传图片(单传)',
|
||||
];
|
||||
$this->result('您正使用CMTS系统文件上传接口!', $apis, 200);
|
||||
}
|
||||
|
||||
public function actionImage()
|
||||
{
|
||||
$post = $this->postdata;
|
||||
$model = new CAttachment();
|
||||
$file = UploadedFile::getInstanceByName('file');
|
||||
//{error: 0
|
||||
// name: "e8.png"
|
||||
// size: 5362
|
||||
// tempName: "/Applications/XAMPP/xamppfiles/temp/phpG8vgnV"
|
||||
// type: "image/png"}
|
||||
// 构建目录
|
||||
$attachDir = Yii::getAlias('@upload'); //附件所在目录
|
||||
|
||||
$targetDir = "/pics"; //开始构建目标转存目录
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$siteId = $this->site_id;
|
||||
$targetDir .= '/'.$siteId;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$pid = $this->pid;
|
||||
$targetDir .= '/'.$pid;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$timeDir = date('Ym');
|
||||
$targetDir .= "/".$timeDir;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
//处理请求的文件信息
|
||||
$title = isset($file->name) ? $file->name : md5(TIMESTAMP.rand(1, 99)).'.jpg'; //文件原名
|
||||
//取后缀名
|
||||
$_filetype = explode('/', $file->type);
|
||||
$suffix = strtolower($_filetype[1]);
|
||||
//重新命名以便转存
|
||||
$newName = date('dHis', TIMESTAMP).'_'.md5($title);
|
||||
$newFile = $attachDir . $targetDir."/".$newName.".".$suffix;
|
||||
|
||||
$res = $file->saveAs($newFile, true);
|
||||
|
||||
//文件入库
|
||||
//POST数据处理
|
||||
$member_id = 0;
|
||||
if (isset($post['mid']) && (int)$post['mid']>0) {
|
||||
$member_id = (int)$post['mid'];
|
||||
}
|
||||
$options = '';
|
||||
if (isset($post['options'])) {
|
||||
$options = json_encode($options, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT);
|
||||
}
|
||||
$model = new CAttachment();
|
||||
$model->siteid = $siteId;
|
||||
$model->pid = $this->pid;
|
||||
$model->uid = $this->user_id;
|
||||
$model->mid = $member_id;
|
||||
$model->type = 0; //附件类型,0图片1视频2音频3表格4文档5PPT888证书999其他
|
||||
$model->status_code = 1;
|
||||
$model->title = $title;
|
||||
$model->filesize = $file->size;
|
||||
$model->route = $targetDir."/".$newName.".".$suffix;
|
||||
$model->options = $options;
|
||||
$model->update_at = $model->create_at = time();
|
||||
$model->save();
|
||||
$id = $model->attributes['id']; //获取插入后id
|
||||
$this->result('上传成功', ['id'=>$id,'path'=>$model->route], 200);
|
||||
}
|
||||
|
||||
public function actionImages()
|
||||
{
|
||||
$post = $this->postdata;
|
||||
$model = new CAttachment();
|
||||
$files = UploadedFile::getInstancesByName('files');
|
||||
// 构建目录
|
||||
$attachDir = Yii::getAlias('@upload'); //附件所在目录
|
||||
|
||||
$targetDir = "/pics"; //开始构建目标转存目录
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$siteId = $this->site_id;
|
||||
$targetDir .= '/'.$siteId;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$pid = $this->pid;
|
||||
$targetDir .= '/'.$pid;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$timeDir = date('Ym');
|
||||
$targetDir .= "/".$timeDir;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
//处理请求的文件信息
|
||||
//POST数据处理
|
||||
$member_id = 0;
|
||||
if (isset($post['mid']) && (int)$post['mid']>0) {
|
||||
$member_id = (int)$post['mid'];
|
||||
}
|
||||
$options = '';
|
||||
if (isset($post['options'])) {
|
||||
$options = json_encode($options, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT);
|
||||
}
|
||||
|
||||
$attachModel = new CAttachment();
|
||||
$ids = $paths = $datas = [];
|
||||
foreach ($files as $file) {
|
||||
$tkey = microtime();
|
||||
$title = isset($file->name) ? $file->name : md5($tkey.rand(1, 99)).'.jpg'; //文件原名
|
||||
//取后缀名
|
||||
$_filetype = explode('/', $file->type);
|
||||
$suffix = strtolower($_filetype[1]);
|
||||
//重新命名以便转存
|
||||
$newName = date('dHis', TIMESTAMP).'_'.md5($title);
|
||||
$newFile = $attachDir . $targetDir."/".$newName.".".$suffix;
|
||||
|
||||
$res = $file->saveAs($newFile, true);
|
||||
|
||||
//文件入库
|
||||
$model = clone $attachModel;
|
||||
$model->siteid = $siteId;
|
||||
$model->pid = $this->pid;
|
||||
$model->uid = $this->user_id;
|
||||
$model->mid = $member_id;
|
||||
$model->type = 0; //附件类型,0图片1视频2音频3表格4文档5PPT888证书999其他
|
||||
$model->status_code = 1;
|
||||
$model->title = $title;
|
||||
$model->filesize = $file->size;
|
||||
$model->route = $targetDir."/".$newName.".".$suffix;
|
||||
$model->options = $options;
|
||||
$model->update_at = $model->create_at = time();
|
||||
$model->save();
|
||||
$id = $model->attributes['id']; //获取插入后id
|
||||
$ids[$id] = $id;
|
||||
$paths[$id] = $model->route;
|
||||
}
|
||||
|
||||
$this->result('上传成功', ['ids'=>$ids,'paths'=>$paths], 200);
|
||||
}
|
||||
|
||||
public function save_image()
|
||||
{
|
||||
$post = Yii::$app->request->post();
|
||||
$model = new CAttachment();
|
||||
$files = UploadedFile::getInstanceByName('files');
|
||||
//{error: 0
|
||||
// name: "e8.png"
|
||||
// size: 5362
|
||||
// tempName: "/Applications/XAMPP/xamppfiles/temp/phpG8vgnV"
|
||||
// type: "image/png"}
|
||||
// 构建目录
|
||||
$attachDir = Yii::getAlias('@upload'); //附件所在目录
|
||||
|
||||
$targetDir = "/pics"; //开始构建目标转存目录
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$siteId = $this->site_id;
|
||||
$targetDir .= '/'.$siteId;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$pid = $this->pid;
|
||||
$targetDir .= '/'.$pid;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$timeDir = date('Ym');
|
||||
$targetDir .= "/".$timeDir;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
//处理请求的文件信息
|
||||
$title = isset($file->name) ? $file->name : md5(TIMESTAMP.rand(1, 99)).'.jpg'; //文件原名
|
||||
//取后缀名
|
||||
$_filetype = explode('/', $file->type);
|
||||
$suffix = strtolower($_filetype[1]);
|
||||
//重新命名以便转存
|
||||
$newName = date('dHis', TIMESTAMP).'_'.md5($title);
|
||||
$newFile = $attachDir . $targetDir."/".$newName.".".$suffix;
|
||||
|
||||
$res = $file->saveAs($newFile, true);
|
||||
|
||||
//文件入库
|
||||
//POST数据处理
|
||||
$post = Yii::$app->request->post();
|
||||
$member_id = 0;
|
||||
if (isset($post['mid']) && (int)$post['mid']>0) {
|
||||
$member_id = (int)$post['mid'];
|
||||
}
|
||||
$options = '';
|
||||
if (isset($post['options'])) {
|
||||
$options = json_encode($options, JSON_UNESCAPED_UNICODE | JSON_FORCE_OBJECT);
|
||||
}
|
||||
$model = new CAttachment();
|
||||
$model->siteid = $siteId;
|
||||
$model->pid = $this->pid;
|
||||
$model->uid = $this->user_id;
|
||||
$model->mid = $member_id;
|
||||
$model->type = 0; //附件类型,0图片1视频2音频3表格4文档5PPT888证书999其他
|
||||
$model->status_code = 1;
|
||||
$model->title = $title;
|
||||
$model->filesize = $file->size;
|
||||
$model->route = $targetDir."/".$newName.".".$suffix;
|
||||
$model->options = $options;
|
||||
$model->update_at = $model->create_at = time();
|
||||
$model->save();
|
||||
$id = $model->attributes['id']; //获取插入后id
|
||||
$this->result('上传成功', ['id'=>$id,'path'=>$model->route], 200);
|
||||
}
|
||||
}
|
||||
|
||||
/*多图上传时的数据示例
|
||||
array(7) {
|
||||
[0]=>
|
||||
object(yii\web\UploadedFile)#60 (6) {
|
||||
["name"]=>
|
||||
string(18) "file-1652511550195"
|
||||
["tempName"]=>
|
||||
string(45) "/Applications/XAMPP/xamppfiles/temp/phpJvR1FV"
|
||||
["type"]=>
|
||||
string(9) "text/html"
|
||||
["size"]=>
|
||||
int(425)
|
||||
["error"]=>
|
||||
int(0)
|
||||
["_tempResource":"yii\web\UploadedFile":private]=>
|
||||
array(0) {
|
||||
}
|
||||
}
|
||||
[1]=>
|
||||
object(yii\web\UploadedFile)#62 (6) {
|
||||
["name"]=>
|
||||
string(18) "file-1652511550195"
|
||||
["tempName"]=>
|
||||
string(45) "/Applications/XAMPP/xamppfiles/temp/phpzIuYVB"
|
||||
["type"]=>
|
||||
string(9) "text/html"
|
||||
["size"]=>
|
||||
int(425)
|
||||
["error"]=>
|
||||
int(0)
|
||||
["_tempResource":"yii\web\UploadedFile":private]=>
|
||||
array(0) {
|
||||
}
|
||||
}
|
||||
[2]=>
|
||||
object(yii\web\UploadedFile)#63 (6) {
|
||||
["name"]=>
|
||||
string(18) "file-1652511550195"
|
||||
["tempName"]=>
|
||||
string(45) "/Applications/XAMPP/xamppfiles/temp/phpUHXR4W"
|
||||
["type"]=>
|
||||
string(9) "text/html"
|
||||
["size"]=>
|
||||
int(425)
|
||||
["error"]=>
|
||||
int(0)
|
||||
["_tempResource":"yii\web\UploadedFile":private]=>
|
||||
array(0) {
|
||||
}
|
||||
}
|
||||
[3]=>
|
||||
object(yii\web\UploadedFile)#64 (6) {
|
||||
["name"]=>
|
||||
string(18) "file-1652511550195"
|
||||
["tempName"]=>
|
||||
string(45) "/Applications/XAMPP/xamppfiles/temp/phpEa5Q93"
|
||||
["type"]=>
|
||||
string(9) "text/html"
|
||||
["size"]=>
|
||||
int(425)
|
||||
["error"]=>
|
||||
int(0)
|
||||
["_tempResource":"yii\web\UploadedFile":private]=>
|
||||
array(0) {
|
||||
}
|
||||
}
|
||||
[4]=>
|
||||
object(yii\web\UploadedFile)#65 (6) {
|
||||
["name"]=>
|
||||
string(18) "file-1652511550195"
|
||||
["tempName"]=>
|
||||
string(45) "/Applications/XAMPP/xamppfiles/temp/phpEDfHIr"
|
||||
["type"]=>
|
||||
string(9) "text/html"
|
||||
["size"]=>
|
||||
int(425)
|
||||
["error"]=>
|
||||
int(0)
|
||||
["_tempResource":"yii\web\UploadedFile":private]=>
|
||||
array(0) {
|
||||
}
|
||||
}
|
||||
[5]=>
|
||||
object(yii\web\UploadedFile)#66 (6) {
|
||||
["name"]=>
|
||||
string(18) "file-1652511550195"
|
||||
["tempName"]=>
|
||||
string(45) "/Applications/XAMPP/xamppfiles/temp/phpiLqBVZ"
|
||||
["type"]=>
|
||||
string(9) "text/html"
|
||||
["size"]=>
|
||||
int(425)
|
||||
["error"]=>
|
||||
int(0)
|
||||
["_tempResource":"yii\web\UploadedFile":private]=>
|
||||
array(0) {
|
||||
}
|
||||
}
|
||||
[6]=>
|
||||
object(yii\web\UploadedFile)#67 (6) {
|
||||
["name"]=>
|
||||
string(18) "file-1652511550195"
|
||||
["tempName"]=>
|
||||
string(45) "/Applications/XAMPP/xamppfiles/temp/php5rqzIk"
|
||||
["type"]=>
|
||||
string(9) "text/html"
|
||||
["size"]=>
|
||||
int(425)
|
||||
["error"]=>
|
||||
int(0)
|
||||
["_tempResource":"yii\web\UploadedFile":private]=>
|
||||
array(0) {
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user