Page 1 of 1

using unlink

Posted: Mon Apr 23, 2012 11:21 am
by nite4000
I am trying to remove a image via from my admin panel

from the admin panel the image is located here

../images/banners/file.png

how ever in my code i have the image name in database so the code would be this

Code: Select all

unlink("../images/banners/" . $banner);
however when i try to delete i get the following error

Warning: unlink(../images/banners/) [function.unlink]: Is a directory in /home/name/public_html/script/admin/promotional.php on line 44

I have the images folder and the banners folder chmod at 777

if anyone knows any ideas to fix this it would be a great help.

Re: using unlink

Posted: Mon Apr 23, 2012 12:06 pm
by x_mutatis_mutandis_x
Looks like your $banner variable is empty, check your query to retrieve the banner. Also, try something like this to avoid getting the warning:

Code: Select all

$banner = fetch_from_DB();

if (!empty($banner)){
    $bannerFile = '../images/banners/' . $banner; 
     if (file_exists($bannerFile)) {
           unlink($bannerFile);
     } else {
           //file not found
     }
} else {
     //fetch query from DB failed, or no banner found in DB
}

Re: using unlink

Posted: Mon Apr 23, 2012 4:57 pm
by pickle
You should also run it through realpath() to ensure you're deleting from the correct directory, just in case $banner turns out to be "../../../path/to/os/file"

Re: using unlink

Posted: Tue Apr 24, 2012 9:07 am
by x_mutatis_mutandis_x
pickle wrote:You should also run it through realpath() to ensure you're deleting from the correct directory, just in case $banner turns out to be "../../../path/to/os/file"
Absolutely! Thanks for pointing it out.