我有一个包含图像和音频文件的文件上传选项的表单。虽然路径在数据库中保存,但图像文件正在保存而不是音频文件。 表单模型:
public $file;
public $binary_file;
public $s_date, $e_date;
public $comment;
public static function tableName()
{
return 'complain';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['complain'], 'required'],
[['complain'], 'string'],
[['file'], 'file', 'extensions' => 'png, jpg', 'maxFiles' => 4],
[['binary_file'], 'file', 'extensions' => 'mp3'],
[['s_date','e_date','complain_date', 'last_updated'], 'safe'],
[['comment'], 'string', 'max' => 255],
[['complain_no'], 'string', 'max' => 20],
];
}
文件实际保存的模型:
public function rules()
{
return [
[['complain_item_id', 'complain_id'], 'required'],
[['complain_item_id', 'complain_id'], 'integer'],
[['file'], 'string', 'max' => 500],
];
}
形式:
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<div class='col-md-12'>
<?= $form->field($model, 'complain')->textarea(['rows' => 6]) ?>
</div>
<?php if ($model->isNewRecord) {?>
<div class='col-md-4'>
<?= $form->field($model, 'file[]')->fileInput(['multiple' => true])->label('Upload Image') ?>
</div>
<div class='col-md-4'>
<?php echo $form->field($model, 'binary_file')->fileInput()->label('Upload Mp3') ?>
</div>
<?php } ?>
<div class="form-group" style='padding-left:14px;'>
<?= Html::submitButton($model->isNewRecord ? 'Submit' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-info submit' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
控制器代码:
public function actionCreate()
{
$model = new Complain();
if ($model->load(Yii::$app->request->post())) {
$model->save(false);
// save the image path to database and save the image to the upload folder
if($model->file = UploadedFile::getInstances($model,'file')) {
$path = 'uploads/img';
FileHelper::createDirectory($path);
foreach($model->file as $fil) {
$modelsComplain= new ComplainItem();
$modelsComplain->save(false);
$fil->saveAs('uploads/img/img'. $fil->extension);
$modelsComplain->file = 'img/img'. $fil->extension;
$modelsComplain->update(false);
}
}
if($model->binary_file = UploadedFile::getInstance($model,'binary_file')) {
$path = 'uploads/mp3';
FileHelper::createDirectory($path);
$modelsComplain= new ComplainItem();
$modelsComplain->save(false);
$model->binary_file->saveAs('uploads/mp3/mp3'. $model->binary_file->extension);
$modelsComplain->file ='mp3/mp3'. $model->binary_file->extension;
$modelsComplain->update(false);
}
return $this->redirect(['view', 'id' => $model->complain_id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
答案 0 :(得分:0)
请使用move_uploaded_file
if(isset($_FILES['binary_file']['tmp_name']) && $_FILES['binary_file']['tmp_name'] != null)
{
$image = $_FILES['binary_file'];
$file = $_FILES;
$new_image['name'] = $_FILES['binary_file']['name'];
$new_image['type'] = $_FILES['binary_file']['type'];
$new_image['tmp_name'] = $_FILES['binary_file']['tmp_name'];
$new_image['error'] = $_FILES['binary_file']['error'];
$new_image['size'] = $_FILES['binary_file']['size'];
$image = $new_image;
$model->binary_file = Yii::$app->params['upload_folder'].time().$file['binary_file']['name']; //set your path
/**
* for uploding image as it is in folder
*/
move_uploaded_file($file['binary_file']['tmp_name'],Yii::getAlias('@webroot')."/".$model->binary_file);
}