如何将多个上传文件的名称保存到 yii 1 中的一个字段
How to save the names of muliple uploaded files to one field in yii 1
我使用 CUploadedFile
在我的网络应用程序中上传多个文件。为此,我使用了以下代码:
public function actionCreate(){
$model=new Status();
$this->performAjaxValidation($model);
if(isset($_POST['Status']))
{
$model->attributes=$_POST['Status'];
Yii::log("actionCreate actionCreate inside if" .isset($_POST['Status']));
$images = CUploadedFile::getInstancesByName('description');
if(isset($images) && count($images)> 0)
{
foreach ($images as $image=>$pic)
{
if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/uploads/'.$pic->name,0777))
{
$model= new Status();
$model->description =$pic->name;
$url = Yii::getPathOfAlias('webroot').'/uploads';
$model->insert();
}
}
$this->redirect(array('view','id'=>$model->status_id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
当我上传多个文件时,它会保存所有文件,但 ID 不同(PK)。我需要将所有上传的文件名保存到一个名为 description
的字段中。我该怎么做?
尝试这样做
public function actionCreate(){
$model=new Status();
$this->performAjaxValidation($model);
if(isset($_POST['Status']))
{
$model->attributes=$_POST['Status'];
Yii::log("actionCreate actionCreate inside if" .isset($_POST['Status']));
$images = CUploadedFile::getInstancesByName('description');
if(isset($images) && count($images)> 0)
{
// Create a blank array.
$pic_name = array();
foreach ($images as $image=>$pic)
{
if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/uploads/'.$pic->name,0777))
{
//Put values in array.
$pic_name[$image] = $pic->name;
$url = Yii::getPathOfAlias('webroot').'/uploads';
}
}
// If array has multiple values i.e. more than zero then and only then save it in description as a string.
if(count($pic_name) > 0){
$_pics = implode('|',$pic_name);
// Do not create model everytime a new image is iterated
$model= new Status();
$model->description = $_pics;
$model->insert();
}
$this->redirect(array('view','id'=>$model->status_id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
我不知道$url在循环中有什么用,所以我一直保持原样。
我使用 CUploadedFile
在我的网络应用程序中上传多个文件。为此,我使用了以下代码:
public function actionCreate(){
$model=new Status();
$this->performAjaxValidation($model);
if(isset($_POST['Status']))
{
$model->attributes=$_POST['Status'];
Yii::log("actionCreate actionCreate inside if" .isset($_POST['Status']));
$images = CUploadedFile::getInstancesByName('description');
if(isset($images) && count($images)> 0)
{
foreach ($images as $image=>$pic)
{
if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/uploads/'.$pic->name,0777))
{
$model= new Status();
$model->description =$pic->name;
$url = Yii::getPathOfAlias('webroot').'/uploads';
$model->insert();
}
}
$this->redirect(array('view','id'=>$model->status_id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
当我上传多个文件时,它会保存所有文件,但 ID 不同(PK)。我需要将所有上传的文件名保存到一个名为 description
的字段中。我该怎么做?
尝试这样做
public function actionCreate(){
$model=new Status();
$this->performAjaxValidation($model);
if(isset($_POST['Status']))
{
$model->attributes=$_POST['Status'];
Yii::log("actionCreate actionCreate inside if" .isset($_POST['Status']));
$images = CUploadedFile::getInstancesByName('description');
if(isset($images) && count($images)> 0)
{
// Create a blank array.
$pic_name = array();
foreach ($images as $image=>$pic)
{
if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/uploads/'.$pic->name,0777))
{
//Put values in array.
$pic_name[$image] = $pic->name;
$url = Yii::getPathOfAlias('webroot').'/uploads';
}
}
// If array has multiple values i.e. more than zero then and only then save it in description as a string.
if(count($pic_name) > 0){
$_pics = implode('|',$pic_name);
// Do not create model everytime a new image is iterated
$model= new Status();
$model->description = $_pics;
$model->insert();
}
$this->redirect(array('view','id'=>$model->status_id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
我不知道$url在循环中有什么用,所以我一直保持原样。