Pick up file without knowing its extension

5

How do I get an image without knowing the length of it? for example:

Image name is moon , but I do not know the extension and when I try to use php commands of the error and says it could not find the file

How can I get a file by name only without knowing the extension?

    
asked by anonymous 19.07.2016 / 18:55

2 answers

8

Try using the glob function

foreach (glob("lua.*") as $archive) {
    // faça sua mágica aqui
}

Or you can also limit extensions

glob("lua.{jpg,png}")

Reference: link

    
19.07.2016 / 19:22
2

You can search all files in a directory using the scandir function, and then comparing the name using the pathinfo function, using the PATHINFO_FILENAME . Feel free to change this script to your needs.

<?php
$dir = '/tmp';
$dirFiles = scandir($dir);
$search = 'lua';

foreach($dirFiles as $file) {
    if (is_file($dir.'/'.$file) AND pathinfo($dir.'/'.$file, PATHINFO_FILENAME) == $search) {
        // aqui você pode fazer o que quiser
        echo "Arquivo encontrado: ".$dir.'/'.$file;
    }
}
?>

Here are links to the php documentation for the functions used:

link

link

    
19.07.2016 / 19:29