This question already has an answer here:
I have image uploaded on the server with the id of customer. But I don't want to upload multiple format of files twice (e.g. there should be only one image for customer id=1 that is 1.jpg OR 1.png)
How can I check globally while updating image whether that file is already there or not?
can I check file exist without extension of file?
I am using this command to check file.
file_exists('./media/customer-id/'.$cus_id);
</div>
Use scandir()
to run a ls
like command and receive an array of the directories contents. Then loop through the files and see if anything matches the customer ID.
$exists = false;
foreach(scandir('./media/customer-id') as $file) {
if(preg_match('/^' . $customer_id . '\.$/', $file)) {
$exists = true;
break;
}
}
You could use glob()
for this and count the result.
function patternExists($pattern) {
return count(glob($pattern)) > 0;
}
and use like this
if (patternExists("./media/customer-id/".$cus_id."*")) {
// bad!
}