public function rules()
{
return [
['contact_file', 'file', 'skipOnEmpty' => false, 'extensions' => 'csv'],
];
}
['contact_file', 'file', 'skipOnEmpty' => false, 'extensions' => ['csv']],
<?php $form = ActiveForm::begin(['method' =--> 'post', 'id' => 'add-contacts-file', 'options' => ['class' => 'mt-2 mb-2 col-lg-4 col-xs-3', 'enctype' => 'multipart/form-data'] ]); ?>
<div class="custom-file">
<?= $form--->field($addFileContact, 'contact_file')->fileInput(['class' => 'custom-file-input'])->label(Yii::t('app', 'Choose file'), ['class' => 'custom-file-label', 'for' => 'customFile']);
?>
<div style="margin-top: -15px"><?= Yii::t('app', 'File type should be .csv') ?></div>
</div>
<?= Html::submitButton(Yii::t('app', 'Add contacts'), ['class' =--> 'mt-2 btn btn-sm button-success', 'form' => 'add-contacts-file']) ?>
<?php ActiveForm::end(); ?>
/**
* @var UploadedFile
*/
public $contact_file;
public function rules()
{
return [
['contact_file', 'file', 'skipOnEmpty' => false, 'extensions' => 'csv'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'contact_file' => 'Contact file',
];
}
public function uploadContacts()
{
if ($this->validate()) {
$this->contact_file->saveAs('uploads/contacts/' . $this->contact_file->baseName . '.' . $this->contact_file->extension);
return true;
} else {
return false;
}
}
$addFileContact = new AddContactsFile();
if (Yii::$app->request->isPost) {
$addFileContact->contact_file = UploadedFile::getInstance($addFileContact, 'contact_file');
if ($addFileContact->uploadContacts()) {
debug('success');
}
}
commented on June 8th 19 at 17:31var filesExt = ['jpg', 'gif', 'png']; // array of extensions
$('input[type=file]').change(function(){
var parts = $(this).val().split('.');
if(filesExt.join().search(parts[parts.length - 1]) != -1){
alert('Good!');
} else {
alert('WTF?!');
}
});
commented on June 8th 19 at 18:07Find more questions by tags Yii