I'm uploading a file through Symfony2 and I am trying to rename original in order to avoid override the same file. This is what I am doing:
$uploadedFile = $request->files;
$uploadPath = $this->container->getParameter('kernel.root_dir') . '/../web/uploads/';
try {
$uploadedFile->get('avatar')->move($uploadPath, $uploadedFile->get('avatar')->getClientOriginalName());
} catch (\ Exception $e) {
// set error 'can not upload avatar file'
}
// this get right filename
$avatarName = $uploadedFile->get('avatar')->getClientOriginalName();
// this get wrong extension meaning empty, why?
$avatarExt = $uploadedFile->get('avatar')->getExtension();
$resource = fopen($uploadPath . $uploadedFile->get('avatar')->getClientOriginalName(), 'r');
unlink($uploadPath . $uploadedFile->get('avatar')->getClientOriginalName());
I am renaming file as follow:
$avatarName = sptrinf("%s.%s", uniqid(), $uploadedFile->get('avatar')->getExtension());
But $uploadedFile->get('avatar')->getExtension()
is not giving me the extension of the uploaded file so I give a wrong filename like jdsfhnhjsdf.
without extension, Why? What is the right way to rename file after or before move to the end path? Any advice?