Jump to content

[SOLVED] How to only use .txt files from a scandir array?


blurredvision

Recommended Posts

I'm using the scandir function to return an array of files with a directory, then I'm using a foreach to create a drop-down box with a list of those files.  This works well and good, but now I only want to return .txt files to populate the drop-down.  I'm not good with expressions (actually, I don't know how to do them at all), so I was hoping somebody could fill in the gap for me here.

 

<?php
$namefill = scandir($preconfdirectory, 0);

foreach ($namefill as $value) {
     if ($value = expression here) {
          echo '<option value="' . $value . '">' . $value . '</option>';
     }
}
?>

 

Thanks for any help!

No need to use complex expressions just use substr

 

foreach ($namefill as $file_name)
{
    $ext = substr($file_name, -4);

    if(strtolower($ext) == '.txt')
    {
        echo '<option value="' . $file_name . '">' . $file_name . '</option>';
    }
}

NOTE: My syntax for substr maybe wrong. I corrected it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.