Warning: unlink(/): Permission denied

Asked

Viewed 1,230 times

1

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

Php code:

   <?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();
        }
 }

?>
  • Post the complete code, so it is easier. We need to know about the variable $photos and $name

  • I already changed the question

  • This is a problem with permissions in the folder, where the file is deleted

  • 1

    The solution is to give the file permission, but I’m even afraid to suggest this, because it’s common for people to open up security when they’re going to do this.

1 answer

1


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

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

To make it work properly you will have to make a list of the items in the database (where the images' records are) and create a link, as for example

http://localhost/delete.php? id=ID-REGISTER-DATABASE-Image=IMAGE.jpg

After doing this, you create a check GET sort of 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 way it will delete the record from the database referring to id and also delete the image with the name that is on image, making a link as an example

http://localhost.php?id=10&imagem=galeria1.jpg

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

You can cut your code to check if the image exists in the directory, if the record with the ID passed also exists in the database, etc. That’s what you decide.

Browser other questions tagged

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