Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.