Warning function unlink is a directory

hi all

i am using this code line to delete image from folder

unlink("../graphics/".$image);

it works fine on my localhost.

But on my live hosting server i m getting this error

Warning: unlink(../graphics/) [function.unlink]: Is a directory in

vineet

Hi @vinpkl

There are two things. It looks as though $image is null, which is why the server is reporting that you are trying to unlink a directory.

Secondly, your live server should be set so as not to report errors as this can be a security flaw.

What versions of PHP do you have (a) on your localhost and (b) on your live server.

both localhost and live have php ver 5.3

vineet

if $image is null then i should write it as

if(file_exists("../graphics/".$image))
{
}

or can you suggest any thing better ??

vineet

i tried again on localhost

i have two images for each product (2nd image is optional).

so some product have one image and some have two images.

so first image exists so it gets deleted always without error

BUT i am getting this error on second image which does not exists

if(file_exists("../graphics/".$image2))
{
unlink("../graphics/".$image2);
}

error

Warning:  unlink(../graphics/) [function.unlink]: Permission denied

But if i am using “file_exists” function then this error should not occur ??

am i using “file_exists” in wrong way ??

vineet

straight from the manual:

file_exists — Checks whether a file or directory exists

but looking at the message it’s obvious that your variable is empty, which shouldn’t be the case, I assume.

yes if file doesnt exists, the variable will be empty.

so what should i do to remove that error ??

vineet

You need to check that it exists and that it is a file - ie it’s not a directory.

so you mean to say that

if variable will be empty then it will assume it as directory because in the path only directory exists

which i think is happening

vineet

Sure thing. In your statement

unlink("../graphics/".$image2)

if $image2 is null, you’re left with

unlink("../graphics/")

which is a directory

thanks for clarifying

i will modify it

vineet

1 Like

The “belt and braces” methods :slight_smile:

Try testing for is_file(), is_dir() or is_link()

http://php.net/manual/en/function.is-file.php

thanks john

vineet

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.