using unlink

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

using unlink

Post 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.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: using unlink

Post 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
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: using unlink

Post 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"
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: using unlink

Post 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.
Post Reply