Home php教程 php手册 Thinkphp框架实现多文件上传实现代码

Thinkphp框架实现多文件上传实现代码

May 25, 2016 pm 04:44 PM
File Upload thumbnail

直接在php中的多文件上传我有介绍过很多并且也写过专题了,但是在thinkphp中多文件上传还是头一次碰到了,那么这个thinkphp多文件上传与php会有什么区别与共同点呢?下面我来给大家介绍一个Thinkphp框架实现多文件上传吧,希望对各位会有点帮助哦。

Thinkphp手册中对于多文件上传描述的很清楚:如果需要使用多个文件,只需要修改表单,把

<input type=&#39;file&#39; name=&#39;photo&#39;>
Copy after login

改为

<li><input type=&#39;file&#39; name=&#39;photo1&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo2&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo3&#39;></li>
Copy after login

或者

<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>
Copy after login
Copy after login


暂且自己的上传表单域为两个,一个上传图片,一个上传视频。字段名为image、video。

html代码如下

图片:<input type="file" name="image[]">
 
视频:<input type="file" name="video[]">
Copy after login

model代码:

<?php
protected $info = &#39;&#39;;
protected $_auto = array(
    array(
        &#39;image&#39;,
        &#39;upload&#39;,
        3,
        callback
    ) , //自动完成方法
    array(
        &#39;video&#39;,
        &#39;videoupload&#39;,
        3,
        callback
    ) , //自动完成方法
    
); //自动填充上传图片生成缩略图
protected function upload() {
    $var = $_FILES[&#39;image&#39;][&#39;name&#39;];
    import(&#39;ORG.Net.UploadFile&#39;);
    $upload = new UploadFile();
    $upload->saveRule = time;
    $upload->allowExts = array(
        &#39;jpg&#39;,
        &#39;gif&#39;,
        &#39;png&#39;,
        &#39;zip&#39;,
        &#39;flv&#39;
    );
    $upload->thumb = true;
    //视频路径。。。只支持flv后缀,
    $upload->videopath = &#39;./Public/upload/Video/&#39;;
    $upload->savePath = &#39;./Public/upload/images/&#39;;
    $upload->thumbPrefix = &#39;250_115_,150_110_,213_156_&#39;;
    $upload->thumbMaxWidth = &#39;250,150,213&#39;;
    $upload->thumbMaxHeight = &#39;115,110,156&#39;;
    if (!in_array(&#39;&#39;, $var) || !in_array(&#39;&#39;, $_FILES[&#39;video&#39;][&#39;name&#39;])) {
        if (!$upload->upload()) {
            echo $upload->getErrorMsg();
            die;
        } else {
            $this->info = $upload->getUploadFileInfo();
            if (!in_array(&#39;&#39;, $var) & amp; & amp;
            !in_array(&#39;&#39;, $_FILES[&#39;video&#39;][&#39;name&#39;])) {
                return $this->info[1][&#39;savename&#39;];
            } elseif (!in_array(&#39;&#39;, $var)) {
                return $this->info[0][&#39;savename&#39;];
            } else {
                return false;
            }
        }
    } else {
        return flase;
    }
}
//上传视频
protected function videoupload() {
    if (!in_array(&#39;&#39;, $var) & amp; & amp;
    !in_array(&#39;&#39;, $_FILES[&#39;video&#39;][&#39;name&#39;])) {
        return $this->info[0][&#39;savename&#39;];
    } elseif (!in_array(&#39;&#39;, $_FILES[&#39;video&#39;][&#39;name&#39;])) {
        return $this->info[1][&#39;savename&#39;];
    } else {
        return false;
    }
}
?>
Copy after login

文章最后我来分析一下多文件上传原理吧,先来看看html代码

<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>
<li><input type=&#39;file&#39; name=&#39;photo[]&#39;></li>
Copy after login
Copy after login

这种就是把表单变量定义为数组,在php中数组特殊变量它可以存储多个不定长的内容,所以我们就可以自定多文件上传框了,那么在php处理时我们要如何操作,下面看例子。

protected $_auto = array(
array(&#39;image&#39;,&#39;upload&#39;,3,callback),//自动完成方法
array(&#39;video&#39;,&#39;videoupload&#39;,3,callback), //自动完成方法
);//自动填充上传图片生成缩略图
Copy after login

这个是告诉thinkphp是数组变量了,并不需要像原生态的php中来判断遍历数组长度再一个个上传的代码了,因为了thinkphp己经做好了。

本文地址:

转载随意,但请附上文章地址:-)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to disable taskbar thumbnail preview in Win11? Turn off the taskbar icon display thumbnail technique by moving the mouse How to disable taskbar thumbnail preview in Win11? Turn off the taskbar icon display thumbnail technique by moving the mouse Feb 29, 2024 pm 03:20 PM

This article will introduce how to turn off the thumbnail function displayed when the mouse moves the taskbar icon in Win11 system. This feature is turned on by default and displays a thumbnail of the application's current window when the user hovers the mouse pointer over an application icon on the taskbar. However, some users may find this feature less useful or disruptive to their experience and want to turn it off. Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another drawback is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. but

How to use Laravel to implement file upload and download functions How to use Laravel to implement file upload and download functions Nov 02, 2023 pm 04:36 PM

How to use Laravel to implement file upload and download functions Laravel is a popular PHP Web framework that provides a wealth of functions and tools to make developing Web applications easier and more efficient. One of the commonly used functions is file upload and download. This article will introduce how to use Laravel to implement file upload and download functions, and provide specific code examples. File upload File upload refers to uploading local files to the server for storage. In Laravel we can use file upload

How to display thumbnails in Vscode_How to display thumbnails in Vscode How to display thumbnails in Vscode_How to display thumbnails in Vscode Apr 02, 2024 pm 02:43 PM

1. First enter Visual Studio Code and click [File] in the upper left corner. 2. Then click [Preferences]. 3. Click the [Settings] item. 4. Then click [Text Editor-Thumbnail]. 5. Finally, in the thumbnail item, turn on [Control whether to display thumbnails].

Implement file upload and download in Workerman documents Implement file upload and download in Workerman documents Nov 08, 2023 pm 06:02 PM

To implement file upload and download in Workerman documents, specific code examples are required. Introduction: Workerman is a high-performance PHP asynchronous network communication framework that is simple, efficient, and easy to use. In actual development, file uploading and downloading are common functional requirements. This article will introduce how to use the Workerman framework to implement file uploading and downloading, and give specific code examples. 1. File upload: File upload refers to the operation of transferring files on the local computer to the server. The following is used

How to use gRPC to implement file upload in Golang? How to use gRPC to implement file upload in Golang? Jun 03, 2024 pm 04:54 PM

How to implement file upload using gRPC? Create supporting service definitions, including request and response messages. On the client, the file to be uploaded is opened and split into chunks, then streamed to the server via a gRPC stream. On the server side, file chunks are received and stored into a file. The server sends a response after the file upload is completed to indicate whether the upload was successful.

How to solve Java file upload exception (FileUploadException) How to solve Java file upload exception (FileUploadException) Aug 18, 2023 pm 12:11 PM

How to solve Java file upload exception (FileUploadException). One problem that is often encountered in web development is FileUploadException (file upload exception). It may occur due to various reasons such as file size exceeding limit, file format mismatch, or incorrect server configuration. This article describes some ways to solve these problems and provides corresponding code examples. Limit the size of uploaded files In most scenarios, limit the file size

PHP file upload guide: How to use the move_uploaded_file function to handle uploaded files PHP file upload guide: How to use the move_uploaded_file function to handle uploaded files Jul 30, 2023 pm 02:03 PM

PHP file upload guide: How to use the move_uploaded_file function to handle uploaded files In developing web applications, file upload is a common requirement. PHP provides a convenient function move_uploaded_file() for processing uploaded files. This article will introduce you how to use this function to implement the file upload function. 1. Preparation Before starting, make sure that your PHP environment has been configured with file upload parameters. You can do this by opening php.in

How to display thumbnails on USB drive How to display thumbnails on USB drive Feb 12, 2024 pm 08:36 PM

I often encounter customers who say that a few minor computer problems are very troublesome at critical times. In summary, there are the following. The reason why the file cannot be copied to the USB flash drive may be that the partition format of the USB flash drive is FAT32 instead of NTFS. You can try changing the partition format of the USB flash drive to NTFS so that you can copy large files. Second, the pictures are not displayed as thumbnails, so you have to click on them one by one to find them. The third type is that the page suddenly becomes larger or smaller. If you don’t know these three situations, they can be solved with a few simple operations. 1. Change the U disk partition to NTFS. The default partition format of a newly purchased U disk is generally FAT32. Under normal circumstances, there is no problem in using it. However, when you need to copy files larger than 4G, a prompt that cannot be copied will appear, which brings some trouble to use. for

See all articles