function no repositório:
public function removeImage (string $filePath): void
{
unlink($filePath); // Deleta a imagem do sistema de arquivos
$sql = 'UPDATE videos SET image_path = NULL WHERE image_path = ?';
$statement = $this->pdo->prepare($sql);
$statement->bindValue(1, $filePath);
$statement->execute();
}
Rota:
'GET | /remover-imagem' => DeleteImageController::class,
Controller:
<?php
namespace Alura\Mvc\Controller;
use Alura\Mvc\Repository\VideoRepository;
class DeleteImageController implements Controller
{
public function __construct(private VideoRepository $videoRepository)
{
}
public function processaRequisicao(): void
{
$filePath = filter_input(INPUT_GET, 'image_path');
if ($filePath === null || $filePath === false) {
header('Location: /?sucesso=0');
return;
}
$success = $this->videoRepository->removeImage($filePath);
if ($success === false) {
header('Location: /?sucesso=0');
} else {
header('Location: /?sucesso=1');
}
}
}
HTML:
<a href="/remover-imagem?image_path=<?= $video->filePath; ?>" >Excluir banner</a>