我正在尝试在我的应用程序中实现多文件上传。我指的是this article并尝试了this example,它有效(基本上,它们是相同的)。但是当我尝试在我的应用程序中实现它时由于某种原因它没有将任何数据保存到数据库表中。
请注意,我也在这里检查了类似的帖子,但他们没有帮助。
因此,当我将文件上传到文件夹(工作正常)时,我需要将filename和sessionID保存到表中。 这是我的表:
CREATE TABLE IF NOT EXISTS `photos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`filename` varchar(45) NOT NULL,
`amount` int(11) DEFAULT NULL,
`pic_size_id` int(11) DEFAULT NULL,
`users_sid` varchar(40) NOT NULL
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
这是我的_form.php
文件:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'photos-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array(
'multiple' => 'multiple',
'enctype' => 'multipart/form-data',
)
)); ?>
<?php echo $form->errorSummary($model); ?>
<?php
$this->widget('CMultiFileUpload', array(
'name'=> 'images',
'accept' => 'jpeg|jpg|gif|png',
'duplicate' => 'Duplicate file!',
'denied'=> 'Invalid file type',
));
?>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
这是我在PhotosController中的actionCreate()
:
public function actionCreate()
{
$model=new Photos;
$images = array();
$images = CUploadedFile::getInstancesByName('images');
if (isset($images) && count($images) > 0)
{
// go through each uploaded image
foreach ($images as $image => $pic)
{
if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/upload/'.$pic->name))
{
$img_add = new Photos();
$img_add->filename = $pic->name; //it might be $img_add->name for you, filename is just what I chose to call it in my model
$img_add->users_sid = Yii::app()->session->sessionID; // this links your picture model to the main model (like your user, or profile model)
if(!$img_add->save()) // save your imagesDONE
{
var_dump($img_add->getErrors());
var_dump($pic->name);
}
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
var_dump($img_add->getErrors())
otuput是:array(1){[“filename”] =&gt; array(1){[0] =&gt; string(25)“文件名不能为空。” }}
var_dump($pic->name)
otuput是:string(37)“windows_metro-wallpaper-1920x1080.jpg”,所以$pic->name
包含文件名。
我真的不知道为什么它没有添加任何东西,当它在相同的例子中起作用。
当我上传一次时它也有效,但当然actionCreate()
和_form.php
略有不同:
PhotosController.php:
public function actionCreate()
{
$model=new Photos;
if(isset($_POST['Photos']))
{
$model->attributes=$_POST['Photos'];
$model->filename=CUploadedFile::getInstance($model,'filename');
if($model->validate())
{
$model->filename->saveAs(Yii::getPathOfAlias('webroot').'/upload/'.$model->filename);
$photo = new Photos;
$photo->filename = $model->filename;
$photo->users_sid = Yii::app()->session->sessionID;
if($photo->save())
{
$this->redirect(array('view','id'=>$model->id));
}
}
}
$this->render('create',array(
'model'=>$model,
));
}
_form.php
:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'photos-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data')
)); ?>
<?php echo $form->errorSummary($model); ?>
<!--<div class="row">
<?php echo $form->labelEx($model,'filename'); ?>
<?php echo $form->fileField($model,'filename'); ?>
<?php echo $form->error($model,'filename'); ?>
</div> -->
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
答案 0 :(得分:0)
a)检查每个$ pic是否为空。 (见我的代码)
b)进行测试,在照片模型规则中将filename-attribute设置为安全
if (isset($images) && count($images) > 0)
{
// go through each uploaded image
foreach ($images as $image => $pic)
{
if(!$pic || !$pic->getName()) {
echo "pic is empty";
continue;
}
if ($pic && $pic->saveAs(Yii::getPathOfAlias('webroot').'/upload/'.$pic->name))
{
// code goes here
}
}