Search code examples
phpglobfile-handling

getting filenname in a combox PHP


i want to get the filenames inside a folder in a combo box. here is my code

$directory = "keys/";

//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");
 echo "yes";
//print each file name
foreach($images as $image)
{
echo $image;
}

it is supposed to show the filenames but it is not showing any filenames.


Solution

  • Try running this it should help you debug your code. You should do debugging in the future.

    /*
     * glob() a pattern inside a directory
     */
    $directory = "keys/";
    $pattern   = "*.jpg";
    
    if (!is_dir($directory)) {
        throw new Exception('Directory was not found!');
    }
    
    $images = glob($directory . $pattern);
    
    if (FALSE === $images) {
        throw new Exception('Glob returned an error.');
    }
    
    if (!$images) {
        throw new Exception('No files returned by Glob. That was not expected.');
    }
    
    // print each file name
    foreach ($images as $image) {
        echo $image;
    }