File upload can directly refer to the framework of the custom file upload class use Think\file;
This encapsulates a file upload model to reuse the uploadfiles.php
1. Controller Layer
Use App\admin\model\uploadfiles; Uploading a model using a file
2. Model Layer
<?php
namespace App\admin\model;
Use Think\model; Using model
Use Think\file; Using File Upload Classes
Use Think\validate; Using File Upload Validation
Use Think\request; Use when you connect to a value
/**
* Package File Upload model
*/
Class Uploadfiles extends Model
{
/**
* Single File Upload
* @param [Type] $file [description]
* @return [Type] string [description]
*/
Public Function Uploadone ($file) {
$filePath = Root_path. ' Public '. Ds. ' Uploads '; Project Physical Path
$rootPath = Request::instance ()->root (true); Project Root Path
if (!file_exists ($filePath)) {
mkdir ($filePath);
}else{
$info = $file
->validate ([
' Size ' =>156780,
' ext ' = ' jpg,png,gif '
])
->move ($filePath);
if ($info) {
Output 20160820/42a79759f284b767dfcb2a0197904287.jpg
return $rootPath. " /uploads/". $info->getsavename (); Returns the picture path with the domain name
Output 42a79759f284b767dfcb2a0197904287.jpg
return $info->getfilename ();
}else{
return $file->geterror ();
}
}
}
/**
* Multi-File Upload
* @param [Type] $files [description]
* @return [Type] array [description]
*/
Public Function Uploadall ($files)
{
$filePath = Root_path. ' Public '. Ds. ' Uploads '; Project Physical Path
$rootPath = Request::instance ()->root (true); Project Root Path
$array = Array ();
foreach ($files as $key = = $file) {
if (!file_exists ($filePath)) {
mkdir ($filePath);
}else{
$info = $file
->validate ([
' Size ' =>156780,
' ext ' = ' jpg,png,gif '
])
->move ($filePath);
if ($info) {
Output 20160820/42a79759f284b767dfcb2a0197904287.jpg
$imgPath = $rootPath. " /uploads/". $info->getsavename (); Returns the picture path with the domain name
Array_push ($array, $imgPath);
Output 42a79759f284b767dfcb2a0197904287.jpg
return $info->getfilename ();
}else{
return $file->geterror ();
}
}
}
return $array;
}
}
TP5 Single file, multi-file upload