PHP check file exists without knowing the extension

Lee picture Lee · Jul 21, 2010 · Viewed 23.9k times · Source

I need to check if a file exists but I don't know the extension.

IE I would like to do:

if(file_exists('./uploads/filename')):
 // do something
endif;

Of course that wont work as it has no extension. the extension will be either jpg, jpeg, png, gif

Any ideas of a way of doing this without doing a loop ?

Answer

Pekka picture Pekka · Jul 21, 2010

You would have to do a glob():

$result = glob ("./uploads/filename.*");

and see whether $result contains anything.