<?php
/**
* 文件上传
* 2019.7.28
* QQ:748507607
*/
namespace app\common\model;
class UploadFile
{
/**
* 单或多图片上传
* @param file_name string 文件名称 name="file_name[]"
* @return url string 多文件或者单文件的url "/uploads/312312.jpg,/uploads/321312.jpg,/uploads/3123123.jpg"
*/
public function uploadfile()
{
$file_name_image = $_FILES['file_name'];
if(empty($file_name_image['name'][0]) || empty($file_name_image['tmp_name'][0]))
return ['code' => '400', 'msg' => '未选择上传图片'];
$i=0;
foreach ($_FILES as $file)
{
if(is_string($file['name']))
{
$files[$i] = $file;
$i++;
}elseif(is_array($file['name']))
{
foreach ($file['name'] as $key => $value)
{
$files[$i]['name'] = $file['name'][$key];
$files[$i]['type'] = $file['type'][$key];
$files[$i]['tmp_name'] = $file['tmp_name'][$key];
$files[$i]['error'] = $file['error'][$key];
$files[$i]['size'] = $file['size'][$key];
$i++;
}
}
}
//处理图片
$file_str = "";
foreach ($files as $k => $v) {
$tmpname = $v['tmp_name']; //文件临时存储路径
$filename = uniqid("Mt").strrchr($v['name'],'.'); //重新命名文件名
$fileurl = ROOT_PATH."public".DS."uploads".DS."images".DS.date('Ymd').DS; //文件存储路径设置
if(!file_exists($fileurl))
{
mkdir($fileurl,0777,true);
}
if(is_uploaded_file($tmpname)){ //临时文件存在
$mvd = move_uploaded_file($tmpname,$fileurl.$filename); //移动到自定义的位置
}
$file_name = str_replace("\\", '/', DS."uploads".DS."images".DS.date('Ymd').DS.$filename);
$file_str .= $file_name.",";
}
$file_str = substr($file_str,0,strlen($file_str)-1);
// echo $file_str;die;
return ['code' => 200, 'msg' => '上传成功', 'url' => $file_str];
}
}