public function handleUploadImageFile(WebsiteOptions $options, UploadedFile $uploadedFile, $imageStr) { if ($uploadedFile->getMimeType() == 'image/png' || $uploadedFile->getMimeType() == 'image/jpg' || $uploadedFile->getMimeType() == 'image/jpeg') { $newFileName = sha1(uniqid(mt_rand(), true)) . '.' . $uploadedFile->guessExtension(); $oldFileName = null; $getImageValue = 'get' . ucfirst($imageStr); $setImageValue = 'set' . ucfirst($imageStr); $uploadDir = $this->webDir . DIRECTORY_SEPARATOR . $options->getUploadDir(); if (!file_exists($uploadDir)) { mkdir($uploadDir, 0777, true); } $realpathUploadRootDir = realpath($uploadDir); if (false === $realpathUploadRootDir) { throw new \Exception(sprintf("Invalid upload root dir '%s'for uploading website images.", $uploadDir)); } if ($options->{$getImageValue}() !== $newFileName) { $oldFileName = $options->{$getImageValue}(); $options->{$setImageValue}($newFileName); try { $uploadedFile->move($realpathUploadRootDir, $newFileName); $this->om->persist($options); $this->om->flush(); } catch (\Exception $e) { if (file_exists($realpathUploadRootDir . DIRECTORY_SEPARATOR . $newFileName)) { unlink($realpathUploadRootDir . DIRECTORY_SEPARATOR . $newFileName); } $options->{$setImageValue}($oldFileName); throw new \InvalidArgumentException($e->getMessage()); } if (null !== $oldFileName && !filter_var($oldFileName, FILTER_VALIDATE_URL) && file_exists($realpathUploadRootDir . DIRECTORY_SEPARATOR . $oldFileName)) { unlink($realpathUploadRootDir . DIRECTORY_SEPARATOR . $oldFileName); } } return array($imageStr => $options->getWebPath($imageStr)); } else { throw new \InvalidArgumentException(); } }