Why yii2 does not pass validation. docx?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
78 views
Something I can not understand, when downloading files do not pass files with extensions docx and txt, and the rest are loaded. And the error occurs during the boot process. Help to understand, what's wrong?
Controller:
<?phpnamespace backend\\controllers;use Yii;use yii\\filters\\AccessControl;use yii\\web\otFoundHttpException;use yii\\filters\\VerbFilter;use yii\\web\\Controller;use backend\\models\\Fileload;use yii\\web\\UploadedFile;class FileloadController extends Controller{/*** @inheritdoc*/public function behaviors(){return ['access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => ['login', 'error'], 'allow' => true, ], [ 'actions' => ['logout', 'index', 'update', 'view', 'create', 'delete'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['POST'], ], ], ]; } public function actionIndex() { $model = new Fileload(); $dir = Yii::getAlias('@uploads') . '/files/'; $name = 'price_titankbr'; if (Yii::$app->request->isPost) { $model->loadFile = UploadedFile::getInstance($model, 'loadFile'); $this->deleteIfExists($dir); if ($model->upload($dir, $name)) { // file is uploaded successfully return $this->redirect(['fileload/index']); } } return $this->render('upload', ['model' => $model]); } private function deleteIfExists($dir) { $files = scandir($dir); $files = array_slice($files,2); //print_r($files); exit; foreach($files as $fl){ unlink($dir . $fl); } } }

Form:
<?phpuse yii\\widgets\\ActiveForm;?><?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?> <?= $form->field($model, 'loadFile')->fileInput()->label('File pricelist') ?> <?php ActiveForm::end() ?>

Model:
<?phpnamespace backend\\models;use Yii;use yii\\base\\Model;use yii\\web\\UploadedFile;class Fileload extends Model{/*** @var loadFile*/public $loadFile;public $maxSize = 1024*1024*2;public $tooBig = 'Файл не должен превышать 2Мб';public function rules(){return [[['loadFile'], 'file', 'skipOnEmpty' => false, 'extensions' => ['pdf', 'doc', 'docx', 'txt', 'xls', 'xlsx'], 'maxSize' => $this->maxSize, 'tooBig' => $this->tooBig], ]; } public function upload($dir, $name) { if ($this->validate()) { $this->loadFile->saveAs($dir . $name . '.' . $this->loadFile->extension); Yii::$app->session->setFlash('success', "Pricelist updated"); return true; } else { return false; } } }
by | 78 views

1 Answer

0 like 0 dislike
The trick is this. There are two ways to determine the file extension:
- mime type
- cutting off the last point
yii by default, the first. But in consequence of those or other reasons the mime type You have to work out clumsily (or not as expected).
So make checkExtensionByMimeType => false perhaps this is enough for You. Or can figure out the MimeType for docx and txt.
\rhttps://www.yiiframework.com/doc/api/2.0/yii-valid...
In summary - the problem outside yii2, dig or use the server method "in the forehead".
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Apr 14, 2019 by staspostnikov
0 like 0 dislike
1 answer
asked May 22, 2019 by khodos_dmitry
0 like 0 dislike
2 answers
asked May 10, 2019 by dmitrylee
110,608 questions
257,187 answers
0 comments
40,796 users