


Detailed use of the multi-image upload plug-in FileInput of the yii2 component, yii2fileinput_PHP tutorial
Jul 12, 2016 am 08:49 AMDetailed 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"
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>
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> 假設(shè)商品的banner圖是 $relationBanners的集合, $id是商品的id // $relationBanners的數(shù)據(jù)結(jié)構(gòu)如:</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 需要預(yù)覽的商品圖,是商品圖的一個(gè)集合 // @param $p2 Array 對(duì)應(yīng)商品圖的操作屬性,我們這里包括商品圖刪除的地址和商品圖的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> 要?jiǎng)h除商品圖的地址</span> 'url' => Url::toRoute('/banner/delete'), <span>//</span><span> 商品圖對(duì)應(yīng)的商品圖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>
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> 需要預(yù)覽的文件格式</span> 'previewFileType' => 'image', <span>//</span><span> 預(yù)覽的文件</span> 'initialPreview' => <span>$p1</span>, <span>//</span><span> 需要展示的圖片設(shè)置,比如圖片的寬度等</span> 'initialPreviewConfig' => <span>$p2</span>, <span>//</span><span> 是否展示預(yù)覽圖</span> 'initialPreviewAsData' => <span>true</span>, <span>//</span><span> 異步上傳的接口地址設(shè)置</span> 'uploadUrl' => Url::toRoute(['/goods/async-banner']), <span>//</span><span> 異步上傳需要攜帶的其他參數(shù),比如商品id等</span> 'uploadExtraData' =><span> [ </span>'goods_id' => <span>$id</span>,<span> ]</span>, 'uploadAsync' => <span>true</span>, <span>//</span><span> 最少上傳的文件個(gè)數(shù)限制</span> 'minFileCount' => 1, <span>//</span><span> 最多上傳的文件個(gè)數(shù)限制</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> 展示圖片區(qū)域是否可點(diǎn)擊選擇多文件</span> 'browseOnZoneClick' => <span>true</span>, <span>//</span><span> 如果要設(shè)置具體圖片上的移除、上傳和展示按鈕,需要設(shè)置該選項(xiàng)</span> 'fileActionSettings' =><span> [ </span><span>//</span><span> 設(shè)置具體圖片的查看屬性為false,默認(rèn)為true</span> 'showZoom' => <span>false</span>, <span>//</span><span> 設(shè)置具體圖片的上傳屬性為true,默認(rèn)為true</span> 'showUpload' => <span>true</span>, <span>//</span><span> 設(shè)置具體圖片的移除屬性為true,默認(rèn)為true</span> 'showRemove' => <span>true</span>,<span> ]</span>,<span> ]</span>, <span>//</span><span> 一些事件行為</span> 'pluginEvents' =><span> [ </span><span>//</span><span> 上傳成功后的回調(diào)方法,需要的可查看data后再做具體操作,一般不需要設(shè)置</span> "fileuploaded" => "<span>function (event, data, id, index) { console.log(data); }</span>",<span> ]</span>,<span> ]); </span>?>
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

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

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)

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

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

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.

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.

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.

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

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

With the rapid development of network technology, our lives have also been greatly facilitated, one of which is the ability to download and share various resources through the network. In the process of downloading resources, magnet links have become a very common and convenient download method. So, how to use Thunder magnet links? Below, I will give you a detailed introduction. Xunlei is a very popular download tool that supports a variety of download methods, including magnet links. A magnet link can be understood as a download address through which we can obtain relevant information about resources.
