Unlink is not working within loop

Asked

Viewed 181 times

0

I have been trying for some time to delete images from a folder through an unlink within a loop,in the case while,but I’m facing the error:

PHP Warning: unlink(admin/imgsupload/32acafa5b1bac0d2af522f76627e3212.jpg ) [Function.unlink]: No such file or directory in /home/Storage/1/05/41/lcjprojetos/public_html/admin/excluirprojeto.php on line 19, referer: http://www.lcjprojetos.com.br/admin/projetos

From what I understand the error is telling me that the directory or file does not exist. However I have already checked and both the directory and the image exist,I tried to give the complete path and no results as well. Follows the code:

<?php
include 'seguranca.php';
include 'conexao.php';
$url = $_SERVER['REQUEST_URI'];
    $parteurl = explode('/', $url);

  $id = $parteurl[3];
$teste=mysqli_real_escape_string($conexao,$id);

    $sql2 = "SELECT * FROM `galeriafotos` WHERE `codigo`=".$teste."";
    $resultado2 = mysqli_query($conexao, $sql2) or die (mysql_error());
    $itens=mysqli_fetch_assoc($resultado2) or die (mysql_error());
unlink("admin/imgsupload/".$itens['imagem']);
$link=$itens['link'];
$sql3 = "SELECT * FROM `fotos` WHERE `link`='$link'";
$resultado3 = mysqli_query($conexao, $sql3) or die (mysql_error());
$itens2=mysqli_fetch_assoc($resultado3) or die (mysql_error());
while($itens2){
    unlink("admin/imgsupload/".$itens2['imagem']);
}


            $sql = "DELETE FROM `fotos`
                    WHERE `link` ='$link'";
            $resultado = mysqli_query($conexao, $sql);
            $sql4 = "DELETE FROM `galeriafotos`
                    WHERE `link` ='$link'";
            $resultado4 = mysqli_query($conexao, $sql4);
            if($resultado4){
                echo "  < <script>window.location.replace('http://www.lcjprojetos.com.br/admin/projetos');</script>";
            }

    ?>

Structure,this code is in the admin folder,where is also the imgsupload folder:

inserir a descrição da imagem aqui

  • Could it be that the directory or file does not exist at the root where the unlink script is running, that is, the above PHP file is at the same level as the admin directory? If possible show the directory structure.

  • I put the structure there Fabio, the strange thing is that the unlink that is out of the while works normally.

  • I don’t know if this is it, but I imagine the problem may be the following: while may be passing a null value, with this returned to unlink a non-existent value. Do the following, play unlink inside an if block and in the Else block put an echo "null value", if the message is returned then instead of the error then we will know that your while is working with null value. Forehead there, then take a closer look (now I’m on the phone so it gets complicated heheh)

  • I did what you told me but did not return the Echo,.

  • a curiosity the path of your image would be this? http://www.lcjprojetos.com.br/admin/imgsupload/32acafa5b1bac0d2af522f76627e3212.jpg

  • while printing the two variables $itens2 and $itens2['image'] and see what returns

  • Yes that same Thalles,the echo of the $itens2 fico = Array and the one of the $itens2['image']=153235afbc0897c23b2fd053b8464df2.jpg

  • the path when accessed by the web ta returning error has protection on that path?

  • When accessed by web displays the image normally,the part of protection I did not understand very well but if that’s what I’m thinking the folder is with permission 775.

  • see the answer I put to see if it helps you.

  • when I try to access through the web your url returns me page error not found

  • 1

    Is the same question?

Show 8 more comments

1 answer

2

I noticed something very important, your file /home/storage/1/05/41/lcjprojetos/public_html/admin/excluirprojeto.php this inside the folder admin, when you perform unlink("admin/imgsupload/".$itens2['imagem']); the path is relative then the unlink will look for a folder called admin within the current admin, the correct would be to use so:

 unlink('imgsupload/'.$itens2['imagem']);

Another detail is that as far as I know mysqli_fetch_assoc should go in the loop loop loop and not out of it, because if not it will just pick up the first item.

while($itens2 = mysqli_fetch_assoc($resultado3)) {
    unlink("imgsupload/".$itens2['imagem']);
}

You can also use file_exists to check if the file exists:

while($itens2 = mysqli_fetch_assoc($resultado3)) {
    if (file_exists("imgsupload/".$itens2['imagem'])) {
        unlink("imgsupload/".$itens2['imagem']);
    }
}

If with file_exists still persist error, you can use clearstatcache, because it is possible that the same file exists more than once in your bank.

Remember that you must process it everywhere there is unlink, thus:

$sql2 = "SELECT * FROM `galeriafotos` WHERE `codigo`=".$teste."";
$resultado2 = mysqli_query($conexao, $sql2) or die (mysql_error());
$itens = mysqli_fetch_assoc($resultado2);

if ($itens && file_exists("imgsupload/".$itens['imagem'])) {
    unlink("imgsupload/" . $itens['imagem']);
}


$link = $itens['link'];
$sql3 = "SELECT * FROM `fotos` WHERE `link`='$link'";

$resultado3 = mysqli_query($conexao, $sql3) or die (mysql_error());

while ($itens2 = mysqli_fetch_assoc($resultado3)) {
    clearstatcache();
    if (file_exists("imgsupload/".$itens2['imagem'])) {
        unlink("imgsupload/" . $itens2['imagem']);
    }
}

Completion

Other than that the code has several problems, as in one place you use while on the other not, the marking is bad of yourself if you guide yourself in what you did and you used die() in some places there was no need and it seems to me that you don’t know very well to use if, while, for, switch which are basic concepts of most programming languages (whether php or not).

  • Please avoid long discussions in the comments; your talk was moved to the chat

Browser other questions tagged

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