Simultaneously delete BD image and server destination folder

1

I'm trying to simultaneously delete a certain image that is in the "logo" field of a table that has several other fields, and the folder where it is stored that is called "upload", but I can only delete the entire row of the table referring to the field "logo", and still does not delete it from the "upload" folder.

The purpose is that when the user decides to change the image, do not get the old images overloading the server, since they are no longer being used.

Here is the code I already have:

   <?php
   include 'conexao.php';
   $pasta = 'upload/';
   if (isset($_POST['deletar'])){
   $check = @$_POST['apagar'];
   foreach($check as $logo){
   $delcheck = mysql_query("DELETE FROM topo WHERE logo = '$logo'") or die (mysql_error());
   unlink($pasta.$delcheck['logo']);
   if ($delcheck >= '1'){
   echo '<script type="text/javascript">
   alert("Deletado com sucesso!");
   window.location.href = "listar.php";
   </script>';
   }else{
   echo '<script type="text/javascript">
   alert("Erro, tente novamente!");
   window.location.href = "listar.php";
   </script>';
   }}}
   ?>

   <form action="" method="POST" enctype="multipart/form-data"><br />
   <?php
   include 'conexao.php';
   $seleciona = "SELECT * FROM topo";
   $queryum = mysql_query($seleciona);
   while ($list = mysql_fetch_array($queryum)){
   $logo = $list['logo'];
   ?>
   <input type="checkbox" name="apagar[]" value="<?php echo $logo; ?>" readonly><?php echo $logo; ?><br />
   <?php
   }
   ?>
   <input type="submit" name="deletar" value="Excluir"><br />
   </form>
    
asked by anonymous 19.09.2015 / 17:29

1 answer

5

There are a number of problems in your script:

  • You should take into consideration the use of relative paths, if the ./upload/ folder is not in the same directory as your script folder, then it will never be able to find the file. I recommend that you always use absolute paths, so create a "global" file (included in the beginning in all scripts) to always point to the correct folder using a variable, for example:

    • global.php

      <?php
      define('ABSOLUTE_PATH', strtr(dirname(__FILE__), '\', '/') . '/');
      

    ABSOLUTE_PATH will display something like c:/wamp/projeto/ or /etc/www/projeto/ or /home/user/htdocs/ . If the upload folder on your server is located in /home/user/htdocs/upload/ , then just use the unlink like this (just an example):

     unlink(ABSOLUTE_PATH . 'upload/arquivo.jpg');
    
  • The unlink can return true if it could be deleted or false otherwise. You should use this to your advantage combined with if , which you did not do.

  • You are foreach , it means that there are several logos you need to delete, but your script uses window.location.href , redirection should only occur after the loop.

  • You have created two variables $_POST['deletar'] and $_POST['apagar'] , you should have used only isset , read this answer I did in another question: link

  • You have used if ($delcheck >= '1'){ , but note that mysql_query does not return numbers or strings but boolean values.

  • Functions that begin with the name mysql_ are deprecated and will soon be discontinued, that is as soon as your server (or your client's server) upgrades to newer versions of PHP your scripts will stop working and will issue several sequences of errors similar to this:

      

    Fatal error: Call to undefined function mysql_connect () in file.php on line 1

    Read about this in this other answers:

  • You are deleting the logos by the image address, where it would be preferable to use the id of the database table.

  • Recommendation: Prefer to use Prepared Statements >

  • 20.09.2015 / 00:32