Php 多文件上传并将数据保存在yii2中的数据库中
在Yii2中上载多个文件时,出现以下错误,无法将数据插入数据库: finfo_文件(C:\xampp\tmp\phpCACD.tmp):无法打开流:没有这样的文件 文件或目录 控制器文件:Php 多文件上传并将数据保存在yii2中的数据库中,php,yii2,image-uploading,Php,Yii2,Image Uploading,在Yii2中上载多个文件时,出现以下错误,无法将数据插入数据库: finfo_文件(C:\xampp\tmp\phpCACD.tmp):无法打开流:没有这样的文件 文件或目录 控制器文件: public function actionGallery($id) { $model = new Gallery(); if (Yii::$app->request->post()) { $model->imageFiles
public function actionGallery($id)
{
$model = new Gallery();
if (Yii::$app->request->post()) {
$model->imageFiles = UploadedFile::getInstances($model,'imageFiles');
foreach ($model->imageFiles as $file) {
$imageName = Yii::$app->security->generateRandomString();
$model->added_date = date('Y-m-d');
$model->emp_id = $id;
$file->saveAs('uploads/emp/' . $imageName . '.' . $file->extension);
$originalFile = EMP_PROFILE_ORIGINAL.$imageName.'.'.$file->extension;
$thumbFile = EMP_PROFILE_THUMB.$imageName.'.'.$file->extension;
Image::thumbnail($originalFile, 200, 200)->save($thumbFile, ['quality' => 80]);
$model->save();
}
}
return $this->render('gallery', [
'gal' => $model
]);
}
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\grid\GridView;
?>
<div class="posts-form">
<div class="wrapper-md">
<div class="row">
<div class="col-sm-8">
<div class="panel panel-default">
<div class="panel-body">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($gal, 'imageFiles[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
<div class="form-group">
<button class="btn btn-success">Submit</button>
<a class="btn btn-default" href="<?=\Yii::$app->urlManager->createUrl('post')?>">Cancel</a>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
</div>
</div>
</div>
查看文件:public function actionGallery($id)
{
$model = new Gallery();
if (Yii::$app->request->post()) {
$model->imageFiles = UploadedFile::getInstances($model,'imageFiles');
foreach ($model->imageFiles as $file) {
$imageName = Yii::$app->security->generateRandomString();
$model->added_date = date('Y-m-d');
$model->emp_id = $id;
$file->saveAs('uploads/emp/' . $imageName . '.' . $file->extension);
$originalFile = EMP_PROFILE_ORIGINAL.$imageName.'.'.$file->extension;
$thumbFile = EMP_PROFILE_THUMB.$imageName.'.'.$file->extension;
Image::thumbnail($originalFile, 200, 200)->save($thumbFile, ['quality' => 80]);
$model->save();
}
}
return $this->render('gallery', [
'gal' => $model
]);
}
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\grid\GridView;
?>
<div class="posts-form">
<div class="wrapper-md">
<div class="row">
<div class="col-sm-8">
<div class="panel panel-default">
<div class="panel-body">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($gal, 'imageFiles[]')->fileInput(['multiple' => true, 'accept' => 'image/*']) ?>
<div class="form-group">
<button class="btn btn-success">Submit</button>
<a class="btn btn-default" href="<?=\Yii::$app->urlManager->createUrl('post')?>">Cancel</a>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
</div>
</div>
</div>
提交
但我正在克服错误。我找不到解决方案。调用$model->save()
以前
$model->file->saveAs();
我遇到了完全相同的问题*我认为您忘记创建上传和emp文件夹,或者您使用了错误的文件夹名称,请检查您的文件夹名称
*如果要保存该文件的名称,应使用不同的变量(两个变量)来保存图像及其名称。首先,在模型中,必须有两个变量来保存图像及其名称
`
* @property string $image_name
*/
class Gallery extends \yii\db\ActiveRecord
{
**public $fileImage=[];**
public static function tableName(){
`
在本例中,我使用$image\u name,它是我的模型列之一,$fileImage,$fileImage是用于上载图像的数组
:)
然后在控制器中
$model = new Gallery(); // this variable is only used to check if everything is valid
if ($model->load(Yii::$app->request->post())) {
$model->fileImage = UploadedFile::getInstances($model,'fileImage');
$a=0;
foreach ($model->fileImage as $file) {
$a++;
$model1 = new Gallery();
$file->saveAs("images/test".$a.".".$file->extension);
$model1->image_url="images/test.".$a.".".$file->extension;
$model1->image_name = "".$a;
$model1->save();
}
return $this->redirect(['index']);
} else {
return $this->render('create', [
'model' => $model,
]);
}
我想就这些 您可以使用
$sql = 'INSERT INTO `table`(`id`, `col1`, `col2`, `path`) VALUES (Null,"'.($model2->col1).'","'.($model2->col2).'","'.($model2->path).'")';
$command = \Yii::$app->db->createCommand($sql);
$command->execute();
而不是$model->save()
我举了个例子,
所以控制器中的整个代码是:
public function actionCreate()
{
$model = new Planet();
if ($model->load(Yii::$app->request->post()) ) {
$model->file = UploadedFile::getInstances($model, 'file');
foreach ($model->file as $file) {
$model2 = new Planet();
$model2->load(Yii::$app->request->post());
$model2->path='uploads/' . $file;
$sql = 'INSERT INTO `planet`(`id`, `name`, `s_id`, `path`) VALUES (Null,"'.($model2->name).'","'.($model2->s_id).'","'.($model2->path).'")';
$command = \Yii::$app->db->createCommand($sql);
$command->execute();
$file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
}
return $this->render('view', [
'model' => $model,
]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
它对我有用
编辑:也可以使用$model2->save(false);它在我的情况下起作用。已经试过了,但不起作用。。它在foreach循环中。。我想将图像的名称保存到数据库中,并且文件夹名称正确。问题是第一个图像被上传到文件夹中,但不会将imagename保存到数据库中。当尝试上载第二张图像时,它会引发错误。我可以在表单中查看您的代码吗?在$model->imageFiles=UploadedFile::getInstances($model,'imageFiles')中;ImageFiles的数据类型是什么??,是$file->saveAs('uploads/emp/'.$imageName.'.$file->扩展名);工作因为您使用了以前从未声明过的$file,所以您说过要在模型中保存多个图像名称,名称是否将保存在不同的行中?如果是,则应声明$model=new Gallery();文件名是否将保存在其他行中?如果是$model=new Gallery(),请在foreach中使用此选项;模型中是否有另一个用于保存图像名称的变量?因为我想你需要它,我会尝试并返回给你图像将进入文件夹,但不在数据库中图像名称保存在$model1->image\u url,,,,,image\u url是我数据库中用于保存文件名称的字段抱歉,我昨天没有internet访问:)这是临时解决方案我想
$model=new Gallery()代码>应该在foreach()循环中