Pick up file without knowing its extension

Posted on

Coding Problem :

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

Answer :

Answer 1 :

Try using the glob function

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

Or you can also limit extensions




Answer 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.

$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:




Answer 3 :

Answer 4 :

Answer 5 :

Answer 6 :

Answer 7 :

Answer 8 :

Answer 9 :

Leave a Reply

Your email address will not be published.