Delete BD and Folder image

0

I am trying to create a script that deletes the image from the database and the folder, but the problem is that it is only deleting from the DB and not from the folder.

Tabela equipes
id, nome, foto

Here is the PHP I'm using:

$id = $_GET["id"];
if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(isset($_POST["deletar"])){
        $sqlDeletar = $pdo->prepare("DELETE FROM equipes WHERE id = :id");
        $sqlDeletar->bindValue(":id", $id);
        $sqlDeletar->execute();
        $dadosDeletar = $sqlDeletar->fetchObject();
        unlink("../upload/".$dadosDeletar->foto);
        echo "Aqui é o link de redirecionar.";
    }else{
        if(isset($_POST["nao"])){
            echo "Aqui é o link de redirecionar.";
        }
    }
}

And here's the form:

<form action="" method="post" enctype="multipart/form-data">
    <label>
        <input type="submit" name="deletar" value="Excluir">
        <input type="submit" name="nao" value="Não excluir">
    </label>
</form>
    
asked by anonymous 12.05.2017 / 14:40

1 answer

0

Check to see if the file exists.

if (is_file("../upload/".$dadosDeletar->foto)) {/*excluir imagem*/} 

Make sure that $dadosDeletar->foto returns the expected value and that you are passing the correct path to the upload folder.

    
12.05.2017 / 14:47