Failed To Set Unsafe Attribute With File Upload

Hello, I’m trying to upload two photos from a form.

In my model the two photos are defined as following:




public $bild1;

public $bild2;


public function rules()

{

  $ruleset = parent::rules();

  $ruleset[] = array('bild1, bild2', 'file', 'maxSize' => 200*200*50, 

    'allowEmpty' => true,);

  $ruleset[] = array('bild1, bild2', 'safe');

  $ruleset[] = array(

      'bild1, bild2', 'EImageValidator', 'allowEmpty' => true,

      'types' => "jpg, png", 'typesError' => 'Falscher Dateityp',

      'width' => 200, 'height' => 200, 'dimensionError' => 'Bild ist nicht 200x200 Pixel groß',

    );

  return $ruleset;

}



In the form I have the following code for the images (the form has the enctype multipart/form-data):




  echo $form->fileField($model, 'bild1', array('id' => 'bild1Field'));

  echo $form->error($model,'bild1');

  echo $form->fileField($model, 'bild2', array('id' => 'bild2Field'));

  echo $form->error($model,'bild2');



The part of the controller looks like this:




  $model = new Musiker;

  if (isset($_POST['Musiker'])) {

    $model->setAttributes($_POST['Musiker']);

...



Every time I try to submit the form, I get the following error


Ungesichertes Attribut "bild1" konnte nicht gesetzt werden.

which means ‘Failed to set unsafe attribute’.

Any suggestions?

Hi,

unset the bild1,bild2 $_POST values - these are not needed after “if (isset($_POST[‘Musiker’]))” (the validator and CUploadedFile use the $_FILES array - here is the reason why the $_POST[‘Musiker’][‘bild1’] and $_POST[‘Musiker’][‘bild2’] are exists: click here).




$model = new Musiker;

  if (isset($_POST['Musiker'])) {

    unset($_POST['Musiker']['bild1'],$_POST['Musiker']['bild2']);

    $model->unsetAttributes();

    $model->setAttributes($_POST['Musiker']);



Sorry for my late answer.

I realized this a little different and didn’t use activeRecord for the uploads but other variables.

But thank you for your answer.