我正在通过 Symfony2 上传文件,并且试图重命名原始文件以避免覆盖相同的文件。这就是我正在做的事情:
$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());
我正在重命名文件如下:
$avatarName = sptrinf("%s.%s", uniqid(), $uploadedFile->get('avatar')->getExtension());
但是$uploadedFile->get('avatar')->getExtension()
没有给我上传文件的扩展名,所以我给了一个错误的文件名,比如jdsfhnhjsdf.
没有扩展名,为什么?在移动到结束路径之后或之前重命名文件的正确方法是什么?有什么建议吗?