Table of Contents
Detailed use of the multi-image upload plug-in FileInput of the yii2 component, yii2fileinput
Home Backend Development PHP Tutorial Detailed use of the multi-image upload plug-in FileInput of the yii2 component, yii2fileinput_PHP tutorial

Detailed use of the multi-image upload plug-in FileInput of the yii2 component, yii2fileinput_PHP tutorial

Jul 12, 2016 am 08:49 AM
fileinput yii2 upload use picture plug-in of components detailed

Detailed use of the multi-image upload plug-in FileInput of the yii2 component, yii2fileinput

Author: Bailang Source: http://www.manks.top/yii2_multiply_images.html The copyright of this article belongs to The author is welcome to reprint, but this statement must be retained without the author's consent, and a link to the original text should be provided in an obvious position on the article page, otherwise the right to pursue legal liability is reserved.

I have also written several articles on file upload, including the most basic yii2 file upload, asynchronous upload to Youpaiyun and the problem of Baidu editor image upload. It seems that it is not perfect if I don’t mention multi-image upload.

Today we introduce FileInput, a multi-image upload plug-in. As for why we chose TA as our upload plug-in, firstly, this product has something to do with Yii2 and is easy to use; secondly, using this plug-in is not only good when adding When operating and modifying, you can also directly delete pictures silently in an asynchronous manner; the most noteworthy thing is that the interface effect incorporates bootstrap, which is refreshing, concise, beautiful, and comfortable to look at.

Use a scene to facilitate the explanation

Suppose we have a product table and a product image table. The product image table only stores product ids and image addresses

Preparation before starting

1. Download the components we need

composer <span>require</span> kartik-v/yii2-widget-fileinput "@dev"
Copy after login

2. Prepare a product table and a product picture table. The product picture table includes the product id and picture url

Synchronous upload of multiple images

What we call synchronous operation here is to select multiple pictures when adding products, and then submit them together with the form. Let’s see how to use it.

<span>use</span> kartik\<span>file</span><span>\FileInput;

</span><span>//</span><span> 非ActiveForm的表单</span>
<span>echo</span> '<label class="control-label">图片</label>'<span>;
</span><span>echo</span> FileInput::<span>widget([
    </span>'model' => <span>$model</span>,
    'attribute' => 'banner[]',
    'options' => ['multiple' => <span>true</span><span>]
]);


</span><span>//</span><span>使用ActiveForm的表单</span>
<span>echo</span> <span>$form</span>->field(<span>$model</span>, 'banner[]')->widget(FileInput::classname(),<span> [
    </span>'options' => ['multiple' => <span>true</span>],<span>
]);</span>
Copy after login

If you want to upload multiple pictures, remember to select multiple pictures when selecting them.

In this way, just submit the form directly after selecting the image. The back-end file upload program needs to be handled by yourself. If you haven't implemented it yet, you can refer to the basic operation of file upload. It should be reminded that, taking this article as an example, when we add multiple pictures to the product here, we actually operate two data tables.

Asynchronous modification (including deletion and addition) operations of product images

As you can see at the beginning, for the product banner image, we upload it along with the form submission. Next, let’s talk about this troublesome matter: how to display the product image when editing the product and how to update the product image. Add and delete operations?

First, we get the banner image corresponding to the product in the controller. Before displaying the banner image on the editing product page, we process it a little:

<span>//</span><span> 假设商品的banner图是 $relationBanners的集合, $id是商品的id
// $relationBanners的数据结构如:</span><span>
/*</span><span>*
 * Array
 *(
 *   [0] => Array
 *        (
 *            [id] => 1484314
 *            [goods_id] => 1173376
 *            [banner] => ./uploads/20160617/146612713857635322241f2.png
 *        )
 *
 *)
 </span><span>*/</span>

<span>$relationBanners</span> = Banner::find()->where(['goods_id' => <span>$id</span>])->asArray()-><span>all();

</span><span>//</span><span> @param $p1 Array 需要预览的商品图,是商品图的一个集合
// @param $p2 Array 对应商品图的操作属性,我们这里包括商品图删除的地址和商品图的id</span>
<span>$p1</span> = <span>$p2</span> =<span> [];
</span><span>if</span> (<span>$relationBanners</span><span>) {
    </span><span>foreach</span> (<span>$relationBanners</span> <span>as</span> <span>$k</span> => <span>$v</span><span>) {
        </span><span>$p1</span>[<span>$k</span>] = <span>$v</span>['banner'<span>];
        </span><span>$p2</span>[<span>$k</span>] =<span> [
            </span><span>//</span><span> 要删除商品图的地址</span>
            'url' => Url::toRoute('/banner/delete'),
            <span>//</span><span> 商品图对应的商品图id</span>
            'key' => <span>$v</span>['id'],<span>
        ];
    }
}

</span><span>return</span> <span>$this</span>->render('banner',<span> [
    </span><span>//</span><span> other params</span>
    'p1' => <span>$p1</span>,
    'p2' => <span>$p2</span>,
    <span>//</span><span> 商品id</span>
    'id' => <span>$id</span>,<span> 
]);</span>
Copy after login

For the code of view file View, please refer to

<span>//</span><span> 视图文件</span>
<span>use</span> kartik\<span>file</span><span>\FileInput;

</span><?<span>php 
</span><span>echo</span> <span>$form</span>->field(<span>$model</span>, 'banner[]')->label('banner图')->widget(FileInput::classname(),<span> [
    </span>'options' => ['multiple' => <span>true</span>],
    'pluginOptions' =><span> [
        </span><span>//</span><span> 需要预览的文件格式</span>
        'previewFileType' => 'image',
        <span>//</span><span> 预览的文件</span>
        'initialPreview' => <span>$p1</span>,
        <span>//</span><span> 需要展示的图片设置,比如图片的宽度等</span>
        'initialPreviewConfig' => <span>$p2</span>,
        <span>//</span><span> 是否展示预览图</span>
        'initialPreviewAsData' => <span>true</span>,
        <span>//</span><span> 异步上传的接口地址设置</span>
        'uploadUrl' => Url::toRoute(['/goods/async-banner']),
        <span>//</span><span> 异步上传需要携带的其他参数,比如商品id等</span>
        'uploadExtraData' =><span> [
            </span>'goods_id' => <span>$id</span>,<span>
        ]</span>,
        'uploadAsync' => <span>true</span>,
        <span>//</span><span> 最少上传的文件个数限制</span>
        'minFileCount' => 1,
        <span>//</span><span> 最多上传的文件个数限制</span>
        'maxFileCount' => 10,
        <span>//</span><span> 是否显示移除按钮,指input上面的移除按钮,非具体图片上的移除按钮</span>
        'showRemove' => <span>true</span>,
        <span>//</span><span> 是否显示上传按钮,指input上面的上传按钮,非具体图片上的上传按钮</span>
        'showUpload' => <span>true</span>,
        <span>//</span><span>是否显示[选择]按钮,指input上面的[选择]按钮,非具体图片上的上传按钮</span>
        'showBrowse' => <span>true</span>,
        <span>//</span><span> 展示图片区域是否可点击选择多文件</span>
        'browseOnZoneClick' => <span>true</span>,
        <span>//</span><span> 如果要设置具体图片上的移除、上传和展示按钮,需要设置该选项</span>
        'fileActionSettings' =><span> [
            </span><span>//</span><span> 设置具体图片的查看属性为false,默认为true</span>
            'showZoom' => <span>false</span>,
            <span>//</span><span> 设置具体图片的上传属性为true,默认为true</span>
            'showUpload' => <span>true</span>,
            <span>//</span><span> 设置具体图片的移除属性为true,默认为true</span>
            'showRemove' => <span>true</span>,<span>
        ]</span>,<span>
    ]</span>,
    <span>//</span><span> 一些事件行为</span>
    'pluginEvents' =><span> [
        </span><span>//</span><span> 上传成功后的回调方法,需要的可查看data后再做具体操作,一般不需要设置</span>
        "fileuploaded" => "<span>function (event, data, id, index) {
            console.log(data);
        }</span>",<span>
    ]</span>,<span>
]);
</span>?>
Copy after login

As mentioned above, we have listed some basic properties and settings of the component FileInput. If necessary, you can check the document for detailed description of the properties.

[Considering that most domestic websites currently collect articles very frequently, and some even do not indicate the source of the original article, the original author hopes that readers can check the original article to prevent any problems and not update all articles to avoid misleading! ]

View original text

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1137575.htmlTechArticleDetailed use of the multi-image upload plug-in FileInput of the yii2 component, yii2fileinput Author: Bailang Source: http://www .manks.top/yii2_multiply_images.html The copyright of this article belongs to the author, welcome to reprint...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

PyCharm Beginner's Guide: Comprehensive understanding of plug-in installation! PyCharm Beginner's Guide: Comprehensive understanding of plug-in installation! Feb 25, 2024 pm 11:57 PM

PyCharm is a powerful and popular Python integrated development environment (IDE) that provides a wealth of functions and tools so that developers can write code more efficiently. The plug-in mechanism of PyCharm is a powerful tool for extending its functions. By installing different plug-ins, various functions and customized features can be added to PyCharm. Therefore, it is crucial for newbies to PyCharm to understand and be proficient in installing plug-ins. This article will give you a detailed introduction to the complete installation of PyCharm plug-in.

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

Share three solutions to why Edge browser does not support this plug-in Share three solutions to why Edge browser does not support this plug-in Mar 13, 2024 pm 04:34 PM

When users use the Edge browser, they may add some plug-ins to meet more of their needs. But when adding a plug-in, it shows that this plug-in is not supported. How to solve this problem? Today, the editor will share with you three solutions. Come and try it. Method 1: Try using another browser. Method 2: The Flash Player on the browser may be out of date or missing, causing the plug-in to be unsupported. You can download the latest version from the official website. Method 3: Press the "Ctrl+Shift+Delete" keys at the same time. Click "Clear Data" and reopen the browser.

What is the Chrome plug-in extension installation directory? What is the Chrome plug-in extension installation directory? Mar 08, 2024 am 08:55 AM

What is the Chrome plug-in extension installation directory? Under normal circumstances, the default installation directory of Chrome plug-in extensions is as follows: 1. The default installation directory location of chrome plug-ins in windowsxp: C:\DocumentsandSettings\username\LocalSettings\ApplicationData\Google\Chrome\UserData\Default\Extensions2. chrome in windows7 The default installation directory location of the plug-in: C:\Users\username\AppData\Local\Google\Chrome\User

See all articles