Code Maniac
Code Maniac

Reputation: 3

How to get the file name extension without actually knowing the extension

I have been trying this PHP code to get the extension of a file without actually knowing it. But I get this error Undefined offset: 0

My file name looks like this: adImage-9-3-1

Is it still possible to get the extension when file name contains one or more hyphens?

This is my PHP code:

    $fileName = "adImage-" . $ImageId . "-" . $adId . "-" . $userId . "*"; //used * to check for other possible results

    $fileInfo = glob($fileName);

    $fileExt = explode(".", $fileInfo[0]); //throws Undefined Offset:0 error
    $fileActualExt = $fileExt[1];

    print_r($fileActualExt); //empty output

I get this error as mentioned above:

Undefined offset: 0

Upvotes: 0

Views: 57

Answers (1)

amir22
amir22

Reputation: 423

You can use this on your unknown file to see its extension:

pathinfo($file, PATHINFO_EXTENSION)

To see the result:

var_dump(pathinfo($file, PATHINFO_EXTENSION));

Upvotes: 3

Related Questions