TP5 Single file, multi-file upload

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.