Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if file exists php

Tags:

php

I use following code to delete old image from ftp...

unlink(getcwd() . '/images/' . $row['pic']);

But it throws errors in case there is not image, so i have tried using file_exists() but that didn't work too, so how can i check if there is an image before trying to delete. thanks.

if(file_exists(getcwd() . '/images/pics/' . $row['pic'])) 
{ 
   unlink(getcwd() . '/images/pics/' . $row['pic']);
}
like image 547
seoppc Avatar asked Apr 08 '26 07:04

seoppc


1 Answers

often hosters use different user for ftp and apache… could it be, that you don't have chmodded your images, so the www-user can't delete them?

edit:

maybe is_writable is the better solution for you:

if(is_writable(dirname(__FILE__) . '/images/pics/' . $row['pic'])){
  unlink(dirname(__FILE__) . '/images/pics/' . $row['pic']); 
}
like image 122
Flask Avatar answered Apr 09 '26 20:04

Flask



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!