<?phpnamespace App\Entity;use App\Repository\ImageSRepository;use Doctrine\ORM\Mapping as ORM;use Symfony\Component\HttpFoundation\File\UploadedFile;use Symfony\Component\Validator\Constraints as Assert;#[ORM\Entity(repositoryClass: ImageSRepository::class)]#[ORM\HasLifecycleCallbacks]#[ORM\Table(name: "ImageS")]class ImageS{ #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: "integer")] private ?int $id = null; #[ORM\Column(type: "string", length: 255)] private ?string $url = null; #[ORM\Column(type: "string", length: 255)] private ?string $alt = null; private ?UploadedFile $file = null; private ?string $tempFilename = null; public function getId(): ?int { return $this->id; } public function getUrl(): ?string { return $this->url; } public function setUrl(string $url): self { $this->url = $url; return $this; } public function getAlt(): ?string { return $this->alt; } public function setAlt(string $alt): self { $this->alt = $alt; return $this; } public function getFile(): ?UploadedFile { return $this->file; } public function setFile(UploadedFile $file): self { $this->file = $file; if (null !== $this->url) { $this->tempFilename = $this->url; $this->url = null; $this->alt = null; } return $this; } #[ORM\PrePersist] #[ORM\PreUpdate] public function preUpload(): void { if (null === $this->file) { return; } $name = $this->file->getClientOriginalName(); $this->url = $name; $this->alt = $name; } #[ORM\PostPersist] #[ORM\PostUpdate] public function upload(): void { if (null === $this->file) { return; } if (null !== $this->tempFilename) { $oldFile = $this->getUploadRootDir() . '/' . $this->tempFilename; if (file_exists($oldFile)) { unlink($oldFile); } } $this->file->move($this->getUploadRootDir(), $this->url); } #[ORM\PreRemove] public function preRemoveUpload(): void { $this->tempFilename = $this->getUploadRootDir() . '/' . $this->url; } #[ORM\PostRemove] public function removeUpload(): void { if (file_exists($this->tempFilename)) { unlink($this->tempFilename); } } public function getUploadDir(): string { return 'uploads/img'; } public function getUploadRootDir(): string { // Adapte ce chemin à Symfony 5 (le répertoire "web/" n'existe plus) return __DIR__ . '/../../public/' . $this->getUploadDir(); }}