Reputation: 1126
I'm working on a site using Symfony3 and you can upload an image for your profile. But I want to check if the image really is an image and if the file is less than 2MB.
The input element already has an accept='image/*'
attribute, so it's only server sided my question.
Here's the code I use to check if the image is really an image less than 2MB:
if(getimagesize($user->getAvatarFile()) !== false) {
if ($organisation->getAvatarFile()->getSize() > 2000000){
$this->addFlash('error', "Image too large!");
}
} else {
$this->addFlash('error', "File is not an image!");
}
the avatarFile
from a user actually is that file, but how do I get it's path so I can use getimagesize
?
Thank you in advance.
Upvotes: 0
Views: 121
Reputation: 766
You should check the symfony doc about File (which is the same as Image, except for mimeType).
There are some constraintslike maxSize, which can be usefull.
use Symfony\Component\Validator\Constraints as Assert;
class User
{
/**
* @Assert\Image(
* maxSize = "2M",
* maxSizeMessage= "The file is too large ({{ size }} {{ suffix }}). Allowed maximum size is {{ limit }} {{ suffix }}."
* )
*/
protected $AvatarFile;
}
EDIT: If you only want to get the file path, before move it, you can use the getClientOriginalName function. Note that it won't return you the full file path as it may be excpected, since on upload, file are renamed on your /tmp dir, and then, moved (and so, renamed if needed).
Upvotes: 1