Warning: unlink (/): Permission denied

1

Good afternoon, I'm trying to delete images from the folder and the database by php, but unfortunately this error occurs: Warning: unlink (/): Permission denied in C: \ xampp \ htdocs \ Troia FS \ php on line 81. I do not know how to fix it. how can I fix this error?

Code php:

   <?php
//error_reporting(0);
  if(isset($_POST['btnEnviar'])){
    $nome = $_FILES['tArq']['name'];
    $formato = $_FILES['tArq']['type'];
    $n = $_POST['nome'];
    $botao = $_POST['botao'];
if (empty($nome)) { echo "<script>alert('Escolha o seu arquivo');</script>"; exit(); } 
if (empty($n)) { echo "<script>alert('Digite o nome da equipe.');</script>"; exit(); } 
    $formatos = array("image/jpg", "image/png", "image/gif", "image/jpeg", "image/bmp", "image/pjpeg");
    $testeType = array_search($formato, $formatos);
    if(!$testeType){
      echo "Formato inválido";
    } else {
      if(file_exists("fotos/$nome")){
        $a = 1;
        while(file_exists("fotos/[$a]$nome")){
          $a++;
        }
        $nome = "[".$a."]".$nome;
      }
      if(move_uploaded_file($_FILES['tArq']['tmp_name'], "fotos/".$nome)){
         $insert = "INSERT INTO mantergaleria('nome','foto') VALUES ('{$n}', '{$nome}')";
        mysql_query($insert);
        echo "<center> Operação Realizada!!! </center>";
      } else {
        echo "<center> Operação Realizada!!! </center>";
        unlink("fotos/$nome");
      }
    }
  }

if($_POST['botao'] == 'Emitir Relátorio' ){
$sql = "SELECT IdGaleria, nome, foto FROM mantergaleria";
$consulta = mysql_query($sql);
echo '<br><br><table width="400px">';
echo '<tr>';
echo '<td><center><b>Id Galeria</td>';
echo '<td><center><b>Nome</td>';
echo '<td><center><b>Foto</td>'; 
echo '</tr>';
while($registro = mysql_fetch_assoc($consulta)){
echo '<tr>';
echo '<td><center>'.$registro["IdGaleria"].'</td>';
echo '<td><center>'.$registro["nome"].'</td>';
echo '<td><center>'.$registro["foto"].'</td>'; 
echo '</tr>'; 
} 
echo '</table>'; 
} 

   if($_POST['botao'] == 'Excluir' ){
 $result = "DELETE from mantergaleria where IdGaleria = '".$_POST['IdGaleria']."'"; 
  unlink($fotos.'/'.$nome); //LINHA 81
     $ver_resultado = @mysql_query($result, $db);
    if ($ver_resultado){
     echo " <center> Operação Realizada!!! </center>";
     }else{
                 echo "Nao foi possivel excluir"; exit();
        echo @mysql_error();
     }
  }

  if($_POST['botao'] == 'Alterar' ){
  $result = "UPDATE mantergaleria SET Nome = '".$_POST['Nome']."' WHERE IdGaleria='".$IdGaleria."'";
    $ver_resultado = @mysql_query($result, $db);
    if ($result){
        echo "<center> Operação Realizada!!! </center>";
     }else{
        echo "Não foi possivel alterar os dados: $Nome, $IdGaleria" ;
        echo mysql_error();
        }
 }

?>
    
asked by anonymous 15.12.2015 / 21:59

1 answer

1

The problem is that the variable $nome will only be valid after a request POST of btnEnviar , that is, it will only be valid in the block of that IF

if(isset($_POST['btnEnviar'])){ //linha 3

In order for you to work correctly you will have to make a list of the database items (where are the records of the images) and create a link, such as

link

After doing this, you create a GET check something like this:

<?php
if(isset($_GET["id"])) AND isset($_GET["imagem"])){

$result = "DELETE from mantergaleria where IdGaleria = '".$_GET['id']."'";

  unlink('fotos/'.$_GET['imagem']); //LINHA 81
     $ver_resultado = @mysql_query($result, $db);
    if ($ver_resultado){
     echo " <center> Operação Realizada!!! </center>";
     }else{
                 echo "Nao foi possivel excluir"; exit();
        echo @mysql_error();
     }

}
?>

This will delete the database record for id and also delete the image with the name that is in image , making a link such as

link

Then it will delete the record with the ID 10 and the image with the name galeria1.jpg

You can fine-tune your code to check if the image exists in the directory, if the record with the passed ID also exists in the database, etc. That's when you decide.

    
15.12.2015 / 22:22