File Type Can't Validate

this is my rule


			

array('picture', 'file', 'types'=>'jpg, gif, png, jpeg', 'allowEmpty'=>true, 'maxSize'=>1024*1024/*1mb*/, 'tooLarge'=>Yii::t('default', 'File is too large'), 'on'=>'create'),



I’m trying to upload multiple images




foreach($_FILES["picture"]["name"] as $key=>$value)

{

    $images->picture='file';

    $images->IDbuyitnow=1;

    if($images->validate())

        echo $value." ";

    else

        echo "doesnt work";

}



HTML code




<?php echo CHtml::fileField('picture[]','',array('class'=>'form-control' ,'multiple'=>true, 'accept'=>'image/*')); ?>



I know I should use CMultiUpload but I wanted to make it like that. But it always validate it even if $images->picture="file" or "25.docx"

Please try following code :

HTML Code

<?php echo $form->fileField($model, ‘picture’); ?>

Model Code

array(‘picture’,‘file’,‘types’=>‘jpg, gif, png, jpeg’, ‘allowEmpty’ => true,‘wrongType’=>‘Only image files can be uploaded’),

array(‘picture’,‘file’, ‘maxSize’=>1024 * 1024 * 5, ‘tooLarge’=>‘File has to be smaller than 2MB’, ‘allowEmpty’ => true),

Please make sure that your form encryption type is : "multipart/form-data"

sounds like a newbie option, but it got me fooled a few times! ;D

If you use active form:


'enctype'=>'multipart/form-data',




           $form=$this->beginWidget('CActiveForm', array(

		'id'=>'post-form',

		'enableClientValidation'=>true,

		'clientOptions'=>array(

			'validateOnSubmit'=>true,

		),

		'htmlOptions'=>array(

			'class'=>'sform',

			'novalidate'=>'novalidate',

			'enctype'=>'multipart/form-data',

		),



It is "multipart/form-data".

[SOLVED] Weirdest thing ever, attribute shouldn’t be called “picture” but “image” or “anything_else” instead :o o.O

FULL SOLUTION

HTML




<?php echo CHtml::fileField('image_file[]','',array('class'=>'form-control' ,'multiple'=>true, 'accept'=>'image/*')); ?>



CONTROLLER

[s]$images=new BuyItNowImage;

$file=CUploadedFile::getInstancesByName(‘image_file’);

foreach($fileas $key=>$value)

{

&#036;images-&gt;image_file=&#036;value;


&#036;images-&gt;IDbuyitnow=1;





if(&#036;images-&gt;validate())


    echo &quot;ok&quot;;


else


    echo &quot;not ok&quot;;

}[/s]




$imagesUpload=CUploadedFile::getInstancesByName('image_file');


if($imagesUpload)

{

    foreach($imagesUpload as $key=>$value)

    {


        $fileName=Yii::app()->user->getId()."_".mt_rand().".".$value->extensionName;

        $images=new BuyItNowImage;

        $images->image_file=$value; ->THIS IS IMPORTANT TO PASS VALUE TO image_file attribute

        $images->IDbuyitnow=1;

        var_dump ($images->validate());


    }

}



MODEL




array('image_file', 'file', 'types'=>'jpg, gif, png, jpeg', 'allowEmpty'=>true, 'maxSize'=>1024*1024/*1mb*/, 'tooLarge'=>Yii::t('default', 'File is too large')),




But do you load the image file or a string ‘file’ or was this just an example?


$images->picture='file';

And does it validate or not? Because in your first post:

And do you know that you can show the errors to find out what it is that not validates?




foreach($_FILES["picture"]["name"] as $key=>$value)

{

    $images->picture='file';

    $images->IDbuyitnow=1;

    if($images->validate())

        echo $value." ";

    else

        print_r($images->getErrors());

}



Finally solved




$imagesUpload=CUploadedFile::getInstancesByName('image_file');


if($imagesUpload)

{

    foreach($imagesUpload as $key=>$value)

    {


        $fileName=Yii::app()->user->getId()."_".mt_rand().".".$value->extensionName;

        $images=new BuyItNowImage;

        $images->image_file=$value; ->THIS IS IMPORTANT TO PASS VALUE TO image_file attribute

        $images->IDbuyitnow=1;

        var_dump ($images->validate());


    }

}