Unlink is not working inside loop [closed]

0

I'm trying for some time to delete images from a folder through an unlink inside a loop , in the case while , however I'm encountering 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: link

From what I understand the error is telling me that the directory or file does not exist. But I have already checked and both the directory and the image exist, I tried to give the complete path and without results as well. Here is 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 the imgsupload folder is also located:

    
asked by anonymous 18.10.2015 / 02:04

1 answer

2

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

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

Another detail is that as far as I know the mysqli_fetch_assoc should go in the loop of the loop and not outside it, because otherwise it will only get 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 you still have the% error, you can use file_exists , since it is possible that the same file exists more than once in your bank.

Remember that you should do this everywhere where clearstatcache exists, like this:

$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']);
    }
}

Conclusion

Apart from this, the code has several problems, as in one place you use unlink in the other, the marking is bad for you to be guided by what you did and you used while in some places that there was no need and It seems to me that you do not know very well how to use die() , if , while , for which are basic concepts of most programming languages (regardless of being php or not).

  • I recommend that you read and study the documentation: link
18.10.2015 / 04:39