darioo
(Timmy Dario)
June 19, 2014, 12:15am
1
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"
khushal
(Khushal Fadiya)
June 19, 2014, 12:02pm
2
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),
khushal
(Khushal Fadiya)
June 19, 2014, 12:03pm
3
Please make sure that your form encryption type is : "multipart/form-data"
yiibjorn
(Bjorn)
June 19, 2014, 12:06pm
4
sounds like a newbie option, but it got me fooled a few times!
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',
),
darioo
(Timmy Dario)
June 19, 2014, 12:15pm
5
It is "multipart/form-data".
[SOLVED] Weirdest thing ever, attribute shouldn’t be called “picture” but “image” or “anything_else” instead 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)
{
$images->image_file=$value;
$images->IDbuyitnow=1;
if($images->validate())
echo "ok";
else
echo "not ok";
}[/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')),
yiibjorn
(Bjorn)
June 19, 2014, 12:28pm
6
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());
}
darioo
(Timmy Dario)
June 20, 2014, 12:38pm
7
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());
}
}