Warning: unlink(img.jpg) [function.unlink]: No such file or directory

I’ve got a deleting images function I picked up from a forum post here:

But im getting this error when I delete an image.

Warning: unlink(img.jpg) [function.unlink]: No such file or directory in /home/s519970/public_html/admin/deletecontent.php on line 20

The image gets deleted but im just assuming its looking for it again and not finding it. Could anyone advise me on how to remove this error?

Thank you!

You may need to write some conditional statement to only delete if the file already exists (and you may need to implement this in your own code, or perhaps modify the function in question).

Also, could you post the function? There is a lot of code at the URL you gave us. If you gave us the code where you call the function that could help as well. Post it using this site’s special PHP tags. Additional context will help me, or if you’re lucky, one of the better programmers around here, to solve your problem.

Here’s the page where I’ve used the code:

[php]<?php
if(isset($_GET[‘delete’]) && $_GET[‘delete’]==‘true’) {
unlink(“img/albumOne/”.$_POST[‘fileToDelete’]);
}
?>
<?php
if ($handle = opendir(“img/albumOne”)) {
while (false !== ($file = readdir($handle)))
{
if ($file != “.” && $file != “…”)
{
$myfile = “img/albumOne/”.$file;
echo “

<a href=“img/albumOne/”.$file.”"><img src=“img/albumOne/”.$file."" width=“100” height=“80”>";
			echo "<form method=\"post\" name=\"deleteSomething\" action="?><?php echo $_SERVER['PHP_SELF'].'?delete=true';?><?php echo" >
			<input type=\"text\" name=\"fileToDelete\" value=".$file." >
			<input type=\"submit\" value=\"Delete\"></form>";
		}   
	}  
	closedir($handle);   	
}  
?>[/php]

I was advised to wrap my unlink in an is_file() condion but they didn’t explain why.

I tried this and got the same error:

[php]is_file(unlink(“img/albumOne/”.$_POST[‘fileToDelete’]));[/php]

Thanks for your help again AnotherWannabe. It is much appreciated. What do you suggest I try. If you could explain any advise your giving me that too would be appreciated. Thanks.

Try:

[php]if(isset($_GET[‘delete’]) && $_GET[‘delete’]==‘true’) {

 if (is_file("img/albumOne/".$_POST['fileToDelete'])) {
          unlink("img/albumOne/".$_POST['fileToDelete']);
 }

}

[/php]
I’ll take another look when I have more time.

Sorted!

Thank you AnotherWannabe!

Sponsor our Newsletter | Privacy Policy | Terms of Service