ThinkPHP多图上传带缩略图功能
功能简介:
支持多图上传并生成缩略图
可自定义缩略图数量与尺寸
文件默认保存在 “./data/attachment/photo/年月/日/” 目录下
访问地址:http://localhost/upload/photo
如有问题请各位大侠海涵并指正,谢谢~
效果:
首先新建一个控制器文件,命名为:UploadController.class.php,
位置:application\Home\Controller\UploadController.class.php
控制器代码:<?php <br />
<br>
/**<br>
* 图片上传控制器<br>
* @author Jiekii <jiekii><br>
* @website http://jiekii.com<br>
* @date 2014-02-21<br>
**/<br>
<br>
namespace Home\Controller;<br>
<br>
class UploadController extends HomeController {<br>
public function photo() {<br>
if(IS_POST) {<br>
//设置文件保存目录<br>
$baseDir = './data/';<br>
$attachDir = './attachment/photo/'.date('Ym').'/';<br>
$subDir = date('d');<br>
$saveName = date('His').strtolower(random(16));<br>
<br>
//上传类配置信息<br>
$config = array(<br>
'maxSize' => 2097152,<br>
'exts' => array('jpg', 'jpeg', 'png', 'gif'),<br>
'rootPath' => $baseDir,<br>
'savePath' => $attachDir,<br>
'subName' => array('date', 'd'),<br>
'saveName' => $saveName,<br>
'hash' => false<br>
);<br>
<br>
//初始化上传类<br>
$upload = new \Think\Upload($config);<br>
<br>
//检查是否选择图片<br>
$inputName = 'photo';<br>
$total = 0;<br>
$data = array();<br>
foreach($_FILES[$inputName] as $key => $value) {<br>
foreach($value as $k => $v) {<br>
$data[$k][$key] = $v;<br>
if($key == 'name' && $v) {<br>
$total++;<br>
}<br>
}<br>
}<br>
<br>
if(!$total) {<br>
$this->error('请先选择要上传的图片!');<br>
}<br>
<br>
$uploadSuccess = $uploadFailure = 0;<br>
$result = array();<br>
<br>
//缩略图列表,数组为空则不生成缩略图<br>
//键为缩略图文件名后缀,例如:20140221abc_a.jpg<br>
//值为缩略图宽/高<br>
$thumbList = array(<br>
'a' => array(150, 150),<br>
'c' => array(250, 250),<br>
'm' => array(500, 500)<br>
);<br>
<br>
//初始化缩略图类<br>
if(!empty($thumbList)) {<br>
$image = new \Think\Image();<br>
}<br>
<br>
foreach($data as $key => $value) {<br>
if(!$value['name']) continue;<br>
<br>
//如果多图则从第二张开始设置新的文件名<br>
if($key >= 1) {<br>
$upload->saveName = date('His').strtolower(random(16));<br>
}<br>
<br>
//开始上传<br>
$file = $upload->upload(array($inputName => $value));<br>
<br>
//上传成功<br>
if(!empty($file)) {<br>
$uploadSuccess++;<br>
<br>
//缩略图<br>
if(!empty($thumbList)) {<br>
$path = $baseDir.$file[$inputName]['savepath'].$upload->saveName;<br>
$fileExt = $file[$inputName]['ext'];<br>
$filePath = $path.'.'.$fileExt;<br>
<br>
//生成缩略图,按照原图的比例<br>
foreach($thumbList as $thumbName => $thumbSize) {<br>
if(!$thumbName || empty($thumbSize)) continue;<br>
<br>
$image->open($filePath);<br>
$image->thumb($thumbSize[0], $thumbSize[1])->save($path.'_'.$thumbName.'.'.$fileExt);<br>
}<br>
}<br>
} else {<br>
$uploadFailure++;<br>
}<br>
$result[] = array($upload->getError(), $file);<br>
}<br>
<br>
//成功提示<br>
if($uploadSuccess) {<br>
$this->success($uploadSuccess.'张图片上传成功!');<br>
} else {<br>
$this->error('上传失败!');<br>
}<br>
} else {<br>
$value = array(<br>
'meta_title' => '上传照片'<br>
);<br>
$this->assign($value)->display();<br>
}<br>
}<br>
}</jiekii>
html代码:<form>
<br>
<p>图片1:<input></p>
<br>
<p>图片2:<input></p>
<br>
<p>图片3:<input></p>
<br>
<p><button>确定上传</button></p>
<br>
</form>
AD:真正免费,域名+虚机+企业邮箱=0元

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

PHP variables store values during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.
