In my form, I have more then 10 file uploads fields,
I have checked file exist though it is giving me error as below
my code is below,
if (file_exists($filename))
unlink($filename);
still it gives me error like below,
unlink(/home/goode/public_html/private/uploads/405/): Is a directory
You can change your code to:
if (is_file($filename)){
unlink($filename);
}
The is_file
function checks if it's a file. file_exists
checks if it's a file OR directory. A bit confusing I will admit.
For example, if
$filename = image.jpg
to proceed with your approach you should use,
if (file_exists(/home/goode/public_html/private/uploads/405/$filename)){
unlink(/home/goode/public_html/private/uploads/405/$filename);
}
if
$filename = /home/goode/public_html/private/uploads/405/image.jpg
you should use,
if (file_exists($filename)){
unlink($filename);
}
Intelligent people always understand the others mean. It's not any big deal.
Of course, file_exists
function can be used to determine whether the exact file exists in a given directory.
I created a demonstration page for those who have misconception about file_exists
function.
I hope it helps others who are eager to learn the facts. A single task can be done in many ways in PHP.
Demo about how PHP copy, file_exists and unlink functions work?