Yii Framework Forum: How to validate file upload field - Yii Framework Forum

Jump to content

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

How to validate file upload field Validate File Upload field

#1 User is offline   dharmalingam 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 16-August 11

Posted 16 August 2011 - 07:09 AM

Hello,

How to validate the file upload field in yii...

The uploaded image is save id in folder not in db.
0

#2 User is offline   rei 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 329
  • Joined: 10-November 10

Posted 20 August 2011 - 08:23 AM

Did you check CFileValidator? You can add validation to your model like this:

array('image', 'file', 'allowEmpty'=>true, 'types'=>'jpg,jpeg,gif,png')

Fipick - Find and pick recommendations
0

#3 User is offline   shailesh 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 79
  • Joined: 14-May 11
  • Location:Ahmedabad(India)

Posted 02 November 2011 - 01:18 AM

For validation File

Just Add

public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
For validation


0

#4 User is offline   rajesh chaurasia 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 12-January 13
  • Location:Mohali

Posted 14 February 2013 - 05:29 AM

hi friend you can also validate your upload image by creating a function in controller
like
public function imageValidate($img, $maxwidth = 1024, $maxheight = 1024)
{

$imagehw = getimagesize($img);
$imagewidth = $imagehw[0];
$imageheight = $imagehw[1];
$imagetype = $imagehw[2];
$valid_types = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP);
if (in_array($imagetype, $valid_types))
{
if($imagewidth < $maxwidth || $imageheight < $maxheight )
return 1;
}

return 0;
}
and checked it in your model by passing the value.
0

#5 User is offline   Blizz 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 14-March 10

Posted 09 April 2013 - 05:18 AM

Rajesh: Don't do stuff like that in the controller, it doesn't belong there as it should know as little as possible of what your model/form needs to work.

You can easily call a custom validation function by using a validation rule like
array('attribute', 'myFunctionName');


It will call the function in the model during validation. You can do that verification either there, or create some sort of file-helper component and call that.
It's a lot cleaner.
0

#6 User is offline   Shakil Hussain 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 12-February 14
  • Location:Dhaka, Bangladesh

Posted 09 March 2014 - 08:28 AM

View Postrei, on 20 August 2011 - 08:23 AM, said:

Did you check CFileValidator? You can add validation to your model like this:

array('image', 'file', 'allowEmpty'=>true, 'types'=>'jpg,jpeg,gif,png')



How can i validate only image extension without page load. when a image upload it will check immediate whether it is match with extension or not. user will get a message instantly. i do it by that way:::::


array('image','file','types'=>"jpg,gif,png",'message'=>'Invalid'),


but it work only after page load.....i want it will check instantly when upload....plz explain with example....
0

#7 User is offline   Shakil Hussain 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 12-February 14
  • Location:Dhaka, Bangladesh

Posted 09 March 2014 - 08:29 AM

View Postshailesh, on 02 November 2011 - 01:18 AM, said:

For validation File

Just Add

public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
For validation



How can i validate only image extension without page load. when a image upload it will check immediate whether it is match with extension or not. user will get a message instantly. i do it by that way:::::

Quote
array('image','file','types'=>"jpg,gif,png",'message'=>'Invalid'),


but it work only after page load.....i want it will check instantly when upload....plz explain with example....
0

#8 User is offline   beingyii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 24-September 13

Posted 31 March 2014 - 08:57 AM

how to get the original name of the image to store in database??
i am using

if($model->save())
			{
				if($model->image !== null)
                        {
						$image=CUploadedFile::getInstance($model,'image');
							$model->image = uniqid() . '.jpg';
                            $image->saveAs( Yii::getPathOfAlias('webroot.images') .  '/'  . '.jpg');
                        }
				$this->redirect(array('view','id'=>$model->addid));
			}


by this it takes the original name in the database but assigns the unique id generated name to the stored image in webroot/image folder

whats wrong in this case??
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