alexs / yii2-fileable
为 Yii2 框架提供可上传文件的扩展。
1.2.1-stable
2021-05-11 19:50 UTC
Requires
- php: >=7.1
- yiisoft/yii2: ^2.0
- yiisoft/yii2-imagine: ~2.1.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-12 03:03:04 UTC
README
为 Yii2 提供上传文件和图片的行为
注意:<br/> 使用 FormData 进行 Ajax 请求.<br/> 不要使用 jQuery Form 插件 http://malsup.com/jquery/form/。它不会将隐藏输入的值发送到服务器.<br/> 如果要删除文件,只需传递值 delete<br/>
例如
<?php
use alexs\yii2fileable\Fileable;
use alexs\yii2fileable\Imageable;
use yii\db\ActiveRecord;
use Yii;
class Article extends ActiveRecord
{
public function rules() {
return [
['image', 'image', 'extensions'=>['jpg', 'jpeg', 'png', 'gif']],
['file', 'file', 'extensions'=>'pdf'],
];
}
// ...
public function behaviors() {
return [
[
'class'=>Fileable::className(),
'upload_dir'=>Yii::getAlias('@uploads_dir') . '/files',
],
[
'class'=>Imageable::className(),
'upload_dir'=>Yii::getAlias('@uploads_dir') . '/images',
'thumbnails'=>[
['width'=>600, 'height'=>600],
['subdir'=>'resized', 'width'=>300, 'height'=>300],
['subdir'=>'cropped', 'width'=>150, 'height'=>150, 'crop'=>true],
],
],
];
}
// ...
}