Yii Framework Forum: Problem Filefield - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem Filefield Rate Topic: ***** 1 Votes

#1 User is offline   bardoo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 30-January 13

Posted 12 February 2013 - 10:17 AM

Hello,
i have a problem in fileField when i click on the submit button there is nothing in the 'nom_fichier' field?
here is my view :
<div class="row">
		<?php echo $form->labelEx($model, 'nom_fichier'); ?>
		<?php echo $form->error($model, 'nom_fichier')?>
		<?php echo $form->fileField($model, 'nom_fichier',array('size'=>45,'maxlength'=>45));?>
	</div>

and my controller is :
if(isset($_POST['Banniere']))
		{
			$model->attributes = $_POST['Banniere'];
			$model->nom_fichier=$_POST['Banniere']['nom_fichier'];
			if($model->validate())
			{
				$uploadedFile = CUploadedFile::getInstance($model, 'nom_fichier');
				$model->save(false);
				$uploadedFile->saveAs("".$dir."/".$model->nom_fichier, $model->nom_fichier);
				
				$this->redirect(array('view','id'=>$model->id));
			}

when i did an echo just after
$model->nom_fichier=$_POST['Banniere']['nom_fichier'];
its empty!
HOPE IS A GOOD THING, MAYBE THE BEST THINGS, AND NO GOOD THING EVER DIES.
0

#2 User is offline   outrage 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 313
  • Joined: 10-November 09
  • Location:Blackpool, United Kingdom

Posted 12 February 2013 - 12:10 PM

Try it this way:

if(isset($_POST['Banniere']))
{
    $model->attributes = $_POST['Banniere'];
    $uploadedFile = CUploadedFile::getInstance($model,'nom_fichier');

    if (is_object($uploadedFile) && get_class($uploadedFile)==='CUploadedFile')
    {
        $model->nom_fichier=$uploadedFile;

        // get the filename if you like
        $filename = $model->nom_fichier->getName();
    }

    if($model->save())
    {
        $uploadedFile->saveAs($dir."/".$filename);
        
        $this->redirect(array('view','id'=>$model->id));

    }

1

#3 User is offline   bardoo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 30-January 13

Posted 13 February 2013 - 04:03 AM

View Postoutrage, on 12 February 2013 - 12:10 PM, said:

Try it this way:

if(isset($_POST['Banniere']))
{
    $model->attributes = $_POST['Banniere'];
    $uploadedFile = CUploadedFile::getInstance($model,'nom_fichier');

    if (is_object($uploadedFile) && get_class($uploadedFile)==='CUploadedFile')
    {
        $model->nom_fichier=$uploadedFile;

        // get the filename if you like
        $filename = $model->nom_fichier->getName();
    }

    if($model->save())
    {
        $uploadedFile->saveAs($dir."/".$filename);
        
        $this->redirect(array('view','id'=>$model->id));

    }


thanks a lot it works like a charm!
HOPE IS A GOOD THING, MAYBE THE BEST THINGS, AND NO GOOD THING EVER DIES.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users