System news PHP - News without error picture and do not delete

Asked

Viewed 28 times

0

Hello!

With the help of some friends here, I set up a news system. But I came across a problem. When I need to delete the news I use the following code:

<?php
    include_once("../../conexao/conexao.php");
    $id = $_GET['id'];
    $diretorio = "../../../style/images/noticias/";  
    $pegaValores = mysqli_query($conn, "SELECT imagem FROM noticias WHERE id = $id");  
    $imagem = mysqli_fetch_object($pegaValores);  
    $imagemQueVaiDeletada = $diretorio . $imagem->imagem;   
    $deleta = unlink($imagemQueVaiDeletada);    
    if($deleta): mysqli_query($conn, "DELETE FROM noticia WHERE id='$id'");  
    endif; 
?>

All Ok, code works, deletes directory image and deletes from Mysql database

But, if there is no image registered in the news nor in the folder.

Generates error on the line $deleta = unlink($imagemQueVaiDeletada);

Warning: unlink(../.../style/images/institutional/): Permission denied

It is not permission problem, it is because the file does not exist in the directory. Because if I fill in the name of the file in the bank and put the image in the directory, delete everything correct, delete the image of the directory and the bank line.

Because there are some news that have no image, neither in the bank nor in the directory.

What check should I do both in the directory and in the bank? I believe that if there is no image in the bank, there will not be in the directory.

To check if there is data in the "image" column of the database. If it is empty delete without going through the delete code in the directory. If there is data in the pass column to delete in the directory.

Someone can help you. Thank you

1 answer

0

The code is online, so it will check line by line. Thus, it would be interesting to check if the file exists. Use something like If(is_file(...)){ ... If there is delete goes into action, otherwise it does nothing. https://www.php.net/manual/en/function.is-file.php

  • Yes, but am I checking in the directory? Like something like this: if(!empty ($_FILES['VARIAVEL)) But I would like to check in the database row. If the table column is empty, delete the row without checking the directory. If the column is data you pass to delete in the directory. Or a simpler solution.

Browser other questions tagged

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