Giter Site home page Giter Site logo

c4ys / yii2-attachments Goto Github PK

View Code? Open in Web Editor NEW

This project forked from philippfrenzel/yii2-attachments

0.0 2.0 0.0 252 KB

Yii2 extension for uploading and attaching the files to the models

Home Page: https://badbreze.github.io/yii2-attachments/

PHP 100.00%

yii2-attachments's Introduction

Yii2 attachments

yii2 文件上传扩展

This fork has the aim to implement some missing features such as multiple fields and simplifying installation

示例

You can see the demo on the krajee website

安装

  1. 修改composer.json文件

      "repositories": [
        {
          "url": "https://github.com/c4ys/yii2-attachments.git",
          "type": "vcs"
        }
      ]
    

    "c4ys/yii2-attachments": "*"
    

    to the require section of your composer.json file.

  2. 添加模块配置

    'modules' => [
    	'file' => [
    		'class' => 'file\FileModule',
    		'webUrl' => '/upload/',
    		'tempPath' => '@common/uploads/temp',
    		'storePath' => '@common/uploads/store',
    		'rules' => [ 
    			'maxFiles' => 20,
    			'maxSize' => 1024 * 1024 * 20 // 20 MB
    		],
    	],
    ],

    添加命令行配置

    'controllerMap' => [
    	'file' => [
    		'class' => 'yii\console\controllers\MigrateController',
    		'migrationPath' => '@file/migrations'
    	],
    ],
  3. 应用数据库迁移

    yii migrate
    
  4. Attach behavior to your model (be sure that your model has "id" property)

    use yii\helpers\ArrayHelper;
    
    /**
     * Declare file fields
     */
    public $my_field_multiple_files;
    public $my_field_single_file;
    
    /**
     * Adding the file behavior
     */
    public function behaviors()
    {
    	return ArrayHelper::merge(parent::behaviors(), [
    		'fileBehavior' => [
    			'class' => \file\behaviors\FileBehavior::className()
    		]
    	]);
    }
    
    /**
     * Add the new fields to the file behavior
     */
    public function rules()
    {
        return ArrayHelper::merge(parent::rules(), [
    		[['my_field_multiple_files', 'my_field_single_file'], 'file'],
        ]);
    }
  5. Make sure that you have added 'enctype' => 'multipart/form-data' to the ActiveForm options

  6. Make sure that you specified maxFiles in module rules and maxFileCount on AttachmentsInput to the number that you want

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.