Alex
Alex

Reputation: 87

How to match file name using php fnmatch or other match expression

I have a little code to match user uploading image name with existing image name, I was able to archive this by using

<?php
$newName = "foo.png"
if(file_exists("images/" . $newName)){
echo "Image already exist";
}
?>

The above code worked for me, but when new image is foo.jpg it will insert new it as new image. So now that i always rename the user image to his username making sure that the file name is same but extension can be different how can i match this? the below code didn't work

<?php
$NewImage = "foo.jpg";
if(fnmatch($_SESSION['username'], $NewImage )) {
  echo "This image already exist";
}
?>

Upvotes: 1

Views: 1149

Answers (1)

lku
lku

Reputation: 1742

It does not work, because you are testing exact match. First argument is The shell wildcard pattern. (as stated in PHP manual), so you need to use wildcard instead of image extension, like this:

$result = fnmatch('foo.*', $fileName);

Upvotes: 2

Related Questions