Reputation: 2654
I have followed the following guide https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md but no file is being uploaded. below is my configuration
_form
This is in a for loop so i can get an array of different records.
<?= $form->field(new UploadForm , "[$count]file")->fileInput()->label(false) ?>
view
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>...
controller
if(isset(Yii::$app->request->post()['Factsheets'])){
for($i=0 ; $i < count(Yii::$app->request->post()['Factsheets']); $i++) {
//Yii::error(print_r(Yii::$app->request->post()['Factsheets'][$i],true));
if(!empty(Yii::$app->request->post()['UploadForm'][$i]['file'])){
$file = new UploadForm();
$file->file = UploadedFile::getInstance(Yii::$app->request->post()['UploadForm'][$i], 'file');
if ($file->file && $file->validate()) {
$file->file->saveAs('uploads/' . $file->file->baseName . '.' . $file->file->extension);
}
}
}
}
post log
[Factsheets] => Array
(
[0] => Array
(
[type] => image
[factsheet_id] => 1185
[path] => ../public/filespool/2/289/Pelotas_Reprocessing.jpg
)
[1] => Array
(
[type] => tech_paper
[factsheet_id] => 1433
[path] => ?basin=pelotas
)
[2] => Array
(
[type] => factsheet
[factsheet_id] => 1844
[path] => ../public/filespool/2/289/Pelotas_Reprocessing.pdf
)
)
[UploadForm] => Array
(
[0] => Array
(
[file] =>
)
[1] => Array
(
[file] =>
)
[2] => Array
(
[file] =>
)
)
I have noticed the following in the post log now. how do i construct it?
$_FILES = [
'UploadForm' => [
'name' => [
0 => [
'file' => 'Destin_Dome.jpg'
]
1 => [
'file' => ''
]
2 => [
'file' => 'Pelotas_Reprocessing.pdf'
]
]
'type' => [
0 => [
'file' => 'image/jpeg'
]
1 => [
'file' => ''
]
2 => [
'file' => ''
]
]
'tmp_name' => [
0 => [
'file' => '/tmp/phpoPgbJ9'
]
1 => [
'file' => ''
]
2 => [
'file' => ''
]
]
'error' => [
0 => [
'file' => 0
]
1 => [
'file' => 4
]
2 => [
'file' => 1
]
]
'size' => [
0 => [
'file' => 1129373
]
1 => [
'file' => 0
]
2 => [
'file' => 0
]
]
]
]
It seems like it is failing validation however i was able to make a copy like:
if(!empty($_FILES['UploadForm']['tmp_name']['file'])){
copy($_FILES['UploadForm']['tmp_name']['file'],"/tmp/".$_FILES['UploadForm']['name']['file']);
}
fileUpload model
public $file;
public $image;
public $factsheet;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['file'], 'file','maxFiles' => 10],
[['image'], 'file','extensions' => 'gif, jpg'],
[['factsheet'], 'file','checkExtensionByMimeType' => false,'extensions' => 'pdf'],
];
}
Upvotes: 2
Views: 4447
Reputation: 2626
I may be wrong but I think the problem is in your controller: your model rules are for multiple file upload and your form field too, but in the controller you set $model->file
as a single UploadedFile
instance. That is why your validation does not work. I can't give your advice (to change controller or model rule) because I do not clearly understand what are you doing here. It looks like you are creating multiple models from a single file upload field...
Upvotes: 2