Grab File without knowing the extension of it

Asked

Viewed 518 times

5

How do I pick up an image even if I don’t know its extension? for example:

Image name is moon, but I don’t know the extension and when I try to use php commands error and says it did not find the file

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

  • what do you mean catch?

  • @durtto Import from my own server

2 answers

8


  • Excepting solution, you can put the link of the Docs of this method: http://php.net/manual/en/function.glob.php

  • Opa, well remembered. I forgot to put the reference.

2

You can search all files from a directory using the function scandir, and then comparing the name using the function pathinfo, using the parameter 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;
    }
}
?>

Follow the php documentation links to the functions used:

http://php.net/manual/en/function.scandir.php

http://php.net/manual/en/function.pathinfo.php

Browser other questions tagged

You are not signed in. Login or sign up in order to post.