Delete image from BD and server destination folder simultaneously

Asked

Viewed 1,068 times

1

I am trying to delete simultaneously 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 every row of the table referring to the "logo" fieldand still does not exclude from the folder "upload".

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

Follow below 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>
  • Dear Friend: Your script is on the same root, because when you unlink in the folder/file upload/IMAGE FILE NAME. Try to put error_reporting(E_ALL); at the beginning of the script to see if an error appears for Voce. Actually this function does not always work, depends on the PHP installation. For it is possible to summarize any error output. No error ? Your PHP is in error mode enabled?

  • Good evening, I wonder if my answer helped you? If not, report might have had some doubt in the use of it.

  • Good evening, I wonder if any of the answers helped you, if not please comment on what you think is missing.

1 answer

5

There are a lot of problems in your script:

  1. You should take into consideration is the use of relative paths, if the folder ./upload/ is not in the same directory as the folder in your script, so it will never be able to find the file. I recommend to always use absolute paths, so create a file "global" (included at 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 on /home/user/htdocs/upload/, then just use unlink like this (just an example):

     unlink(ABSOLUTE_PATH . 'upload/arquivo.jpg');
    
  2. The unlink can return true whether it has succeeded in excluding or false otherwise. You must use this to your advantage combined with if, what you didn’t do.

  3. You are foreach, understand that there are several logos that you need to delete, but your script uses window.location.href, redirecting should only occur after the loop.

  4. You created two variables $_POST['deletar'] and $_POST['apagar'], you should have used only the isset, read this answer I asked in another question: /a/63550/3635

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

  6. Functions starting with the name mysql_ are in disuse and soon will be discontinued, or so your server (or your clients' server) update to newer versions of PHP your scripts will stop working and will emit several error sequences similar to this:

    Fatal error: Call to Undefined Function mysql_connect() in file.php on line 1

    Read about this in this other answers:

  7. You are deleting the logos by the image address, when the preferable would be to use the database table id.

  8. Recommending: Prefer to use Prepared Statements

Browser other questions tagged

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