[英]How to get path from file variable in Symfony3
I'm working on a site using Symfony3 and you can upload an image for your profile. 我正在使用Symfony3的网站上,您可以为个人资料上传图片。 But I want to check if the image really is an image and if the file is less than 2MB. 但是我想检查图像是否真的是图像,文件是否小于2MB。
The input element already has an accept='image/*'
attribute, so it's only server sided my question. 输入元素已经具有accept='image/*'
属性,因此它仅是服务器方面的问题。
Here's the code I use to check if the image is really an image less than 2MB: 这是我用来检查图像是否真正小于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
? 用户的avatarFile
实际上是该文件,但是如何获取它的路径,以便可以使用getimagesize
?
Thank you in advance. 先感谢您。
You should check the symfony doc about File (which is the same as Image , except for mimeType). 您应该检查有关File的symfony文档(与mimeType相同,与Image相同)。
There are some constraintslike maxSize , which can be usefull. 有一些限制,例如maxSize ,可能很有用。
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. 编辑:如果只想获取文件路径,则在移动它之前,可以使用getClientOriginalName函数。 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). 请注意,它不会返回您期望的完整文件路径,因为在上载时,文件在/ tmp目录中被重命名,然后被移动(并在需要时进行重命名)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.