Unlink for multiple files

Asked

Viewed 342 times

0

I am trying to use unlink to delete from a directory several images do not know what is happening apparently the logic is correct, my method is as follows:

public function deleteImagem($id) {
    $selectDasImagens = PDOUtil::getStance()->prepare("select nome FROM imagem where id_pagina=:id");
    $selectDasImagens->bindValue(":id", $id);
    $selectDasImagens->execute();
    while ($linha = $selectDasImagens->fetch(PDO::FETCH_OBJ)) {
        echo unlink("../uploaded/$linha->nome");
    }
}

someone has been there.

  • you want to delete multiple files? $linha->nome comes with the extension?

  • yes already comes with the name and yes I want to delete several files, the interesting thing is that when I do without while it erases a file quietly.

  • 1

    echo writes zero(false)?

  • that’s right, correct answer.

  • You may not be allowed to delete the file, or name is not correct, try with: echo unlink("../uploaded/{$linha->nome}");. You can also delete passand the full file path.

1 answer

1


solved as follows:

I first opened the terminal and edited the directory permissions: chmod 777 then I removed the echo from the method.

public function deleteImagem($id) {
$selectDasImagens = PDOUtil::getStance()->prepare("select nome FROM imagem where id_pagina=:id");
$selectDasImagens->bindValue(":id", $id);
$selectDasImagens->execute();
while ($linha = $selectDasImagens->fetch(PDO::FETCH_OBJ)) {
    unlink("../uploaded/$linha->nome");
}

}

thanks @rray

  • Just removed echo? or was permission the problem?

  • I gave a chmod 777 in the directory too

  • That one reply may help to better understand what happened, is a related problem.

  • thank you very much I will read everything.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.