0

I want to grab the file name from a known base name in PHP.

I've searched and found pathinfo(), but then you already give the file name with base name and extension in the argument.

Also in the pathinfo() docs:

$path_parts = pathinfo('/path/basename');
var_dump($path_parts['extension']);

That's more or less what I need, but the result is :

string '' (length=0)

I want the extension, not an empty string. I know the base name, but I don't know the extension, how do I grab that?

Sylwester
  • 47,942
  • 4
  • 47
  • 79
Maurice
  • 1,342
  • 11
  • 21
  • 1
    So you want the extension of an extension-less file? (facepalm, facepunch) – CodeAngry Jul 07 '13 at 18:20
  • pathinfo is just a internal parser, if the filename has no extension then there is no extension. If you looking to get a filetype from a remote file then your should look for the Content-Type: header and use that to define your extension. – Lawrence Cherone Jul 07 '13 at 18:20
  • And if the filename has an extension, then you know it. You just need to `grab` ;) – brasofilo Jul 07 '13 at 18:21
  • ah I think you mean you need file type – Fallen Jul 07 '13 at 18:21
  • That file just dosent have an extension lol, its impossible to get it. – Lemon Drop Jul 07 '13 at 18:21
  • The file has a extension (png or jpg etc.). I only know the name of the file. – Maurice Jul 07 '13 at 18:22
  • if that file has an extension, how come the path looks like, **$path_parts = pathinfo('/path/noextension');** ? Where is the **.type**? part of the path? – Fallen Jul 07 '13 at 18:23
  • The extension is _part_ of the name of the file. – Barmar Jul 07 '13 at 18:25
  • Are you sure your question is correct? It seems to me that you are interested in filetype of a file without extension. Probably you should ask how to determin filetype from content? – Sylwester Jul 07 '13 at 18:27
  • Am I the only one thinking he should be dealing with mime-types here? – nand Jul 07 '13 at 18:35
  • I thougt the part after the dot is called extension? And a image can be different types, wich have different extensions for that reason? Maybe i`m wrong. But the file i need is a image, and offcourse they have an extension(type), but its dynamic, so i don`t know it. Joni's answer is what i needed. – Maurice Jul 07 '13 at 18:37

3 Answers3

0

To list file names, knowing only the base, you can use glob. It returns an array because there may be several files with the same base but different extensions, or none at all:

$files = glob($base.'.*');
if (!empty($files))
    echo $files[0];
Joni
  • 108,737
  • 14
  • 143
  • 193
  • Thanks! This is what i was looking for. It will give me the full path of the image, while not knowing its extension (.*) – Maurice Jul 07 '13 at 18:30
0

If file has an extension you can get it like this

$file_name = 'test.jpg';
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
echo $ext;  // output:jpg

Example: To scan all files in images directory and show if ext is allowed or not

$image_path = __DIR__ . '/images/';
$files = scandir($image_path);
$allowed_images = array('jpg', 'jpeg', 'gif', 'png');
foreach ($files as $file_name) {
    $ext = pathinfo($file_name, PATHINFO_EXTENSION);
    if (in_array($ext, $allowed_images)) {
        echo $ext . ' allowed<br>';
    } else {
        echo $ext . ' not allowed<br>';
    }
}
Manoj Yadav
  • 6,560
  • 1
  • 23
  • 21
  • This only works if you hard code the extension(type) in your script (.jpg in this example) – Maurice Jul 07 '13 at 18:39
  • I have hard coded the extension for demo. It will work for dynamic extension also, only requirement is that file should have an extension. It doesn't matter if it's hard coded or dynamically generate. I have updated the answer to scan images directory and show the result. – Manoj Yadav Jul 07 '13 at 18:59
-1

You could use:

$ext = end(explode('.',$filename));

Note: it's not secure to rely on this extension to determine the file type.

Andy Gee
  • 3,149
  • 2
  • 29
  • 44