Removing an image via unlink does not work

Asked

Viewed 1,503 times

1

I have the following code below:

//carrega a imagem anterior
$img = $db->prepare("select ipco_descr_multimidia,ipco_arquivo from ipco_multimida where ipco_id_questao_fk = :id ");
$img->bindParam(':id',$id);

//executa a query
$img->execute();

//recupera os valores da tabela
$resultado = $img->fetchAll();
$caminho = $resultado['ipco_arquivo'];
$nome = $resultado['ipco_descr_multimidia'];

//apagar o arquivo antigo da pasta
if(file_exists($caminho.$nome)) {
    @unlink($caminho.$nome);
    //exit;
}

Multimedia table:

ipco_descr_multimidia | ipco_arquivo
1413297340.jpg        | ./img/upload/

When I run the code, it does not erase the image inside the img/upload folder.

  • 2

    Start by removing the @ which removes the error in unlink and maybe you get to have more clues than is wrong...

  • 2

    The problem is more related to removing the file from the database. Check the folder permissions. Put the operating system you are using.

  • 1

    Solved, missing add position of $ipco_descr_multimidia array = $result[0]['ipco_descr_multimidia'];

  • 1

    Is he finding the file? That is, the instruction file_exists($caminho.$nome) is returning TRUE? It can also be a location problem. As your folder structure is, the file you are running is in the same location as the folder img?

  • @hulckb put as answer the procedure you performed to solve the problem.

  • 4

    CARING The function of PHP file_exists() return TRUE if the path ends in a directory, that is, if the variable $nome is empty, the current code enters the same IF. Use the function is_file() is the safest way to check whether the file exists.

Show 1 more comment

1 answer

4


According to your code, there are some things that can be improved so that there is greater coherence between what you want and what you are doing. On the other hand, some of the suggestions aim to solve potential security problems:

  1. Use the PDO statement fetch() that returns a line instead of the fetchAll().

    This point solve the problem that prompted you to ask the question.

  2. Uses the function is_file() to check if the file exists because it only returns TRUE if there is a file and it is actually a file.

  3. Check the values before using them, as you can see below using the function empty().

  4. Do not hide system errors, your code should be prepared to handle them. The function unlink() return FALSE when something goes wrong. If something went wrong, you should act accordingly either by informing the user or by taking alternative measures for the task in question.

Example of your code with the above:

/* Carrega a imagem anterior
 */
$img = $db->prepare("
  SELECT ipco_descr_multimidia, ipco_arquivo
  FROM ipco_multimida
  WHERE ipco_id_questao_fk = :id
");
$img->bindParam(':id', $id);

// executa a query
$img->execute();

// recupera os valores da tabela
$resultado = $img->fetch();

$caminho = $resultado['ipco_arquivo'];
$nome = $resultado['ipco_descr_multimidia'];

/* apagar o arquivo antigo da pasta
 */
if (!empty($caminho) && !empty($nome) && is_file($caminho.$nome)) {

    if (!unlink($caminho.$nome)) {
      die("Ups... correu mal!");
    }
}
  • 1

    Thank you very much Zuul

Browser other questions tagged

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