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:
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.
– Fábio Jânio
I put the structure there Fabio, the strange thing is that the unlink that is out of the while works normally.
– Vinicius Zable
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)
– Fábio Jânio
I did what you told me but did not return the Echo,.
– Vinicius Zable
a curiosity the path of your image would be this? http://www.lcjprojetos.com.br/admin/imgsupload/32acafa5b1bac0d2af522f76627e3212.jpg
– Thalles Daniel
while printing the two variables $itens2 and $itens2['image'] and see what returns
– Thalles Daniel
Yes that same Thalles,the echo of the $itens2 fico = Array and the one of the $itens2['image']=153235afbc0897c23b2fd053b8464df2.jpg
– Vinicius Zable
the path when accessed by the web ta returning error has protection on that path?
– Thalles Daniel
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.
– Vinicius Zable
see the answer I put to see if it helps you.
– Thalles Daniel
when I try to access through the web your url returns me page error not found
– Thalles Daniel
Let’s go continue this discussion in chat.
– Thalles Daniel
Is the same question?
– rray