0

I'm currently making a droplist but in the droplist let's say I only want to include only .txt extension files so any other extensions like .php .jpg or any other extensions will not be in in the droplist. How can I do that as simple as possible?

Another question is I want to make a warning IF the folder does not have any .txt extension files an error message will show. So even if there are other .jpg .php or any other files inside as long as there's no .txt file in the folder a warning will show.

Anyone able to give me a hand?

This is what I have done but it only shows a drop-list with no .txt at the end but it will still show other random files in the drop-list though.

if(!(is_dir("./aaa")))
{
    die("Must create a folder first, sorry");
}

    $lists = scandir("./aaa");

    echo "<form action=\"./page2.php\" method=\"get\">";
    echo "<select>";

    foreach($lists as $list)
    {
    if(($list == ".") || ($list == ".."))
        {
        continue;
        }
    echo "<option value=\"";
    echo basename($list,".txt");
    echo "\">";
    echo basename($list,".txt");
    echo "</option>";
    }
    echo "</select>";
    echo "</form>";

editted added the substr with $hasTxt

<?php
if(!(is_dir("./aaa")))
{
    die("Must create a <strong>aaa</strong> folder first, sorry");
}
echo "<form action=\"./page2.php\" method=\"get\">";
echo "<select name=\"aaa\">";
    $aaa_files = scandir("./aaa"); 
$hastxt = false; 
foreach($aaa_files as $file_list) 
{
    if(($file_list == ".") || ($file_list == ".."))
    {
    continue;
    }
    if(strlen($file_list)>4 && strtolower(substr($file_list, -4))!='.txt')
    {
    continue;
    }
    else 
    {
    $hastxt = true; 
    echo "<option value=\"";
    echo basename($file_list,".txt");  
    echo "\">";
    echo basename($file_list,".txt");
    echo "</option>";
    }
}
echo "</select>";
echo "<br/><input type=\"submit\">";
echo "</form>";

if($hastxt == false) 
{
    echo "Must create text files first, sorry";
    die();
}
?>

This is what happens for the script that I have now if the folder does not have any txt files.

enter image description here


This is what I actually want if there's no txt file but of course without the arrow

enter image description here

3 Answers 3

1

For the first part, just like you continue on directories . and .., you can continue on non-text files:

if(strlen($list)>4 && strtolower(substr($list, -4))!='.txt') continue;

For the warning part, put a flag before the foreach

$hasTxt = false;

And set it to true whenever you get input you don't ignore (ie. after the if(unwanted) continue;)

$hasTxt = true;

Finally, after the foreach check the value of $hasTxt and use it as you prefer.

15
  • ah....right....I do understand those but I wonder what if someone named the file TxT or TXT because I used strtolower as you said and I changed one of the file to .TXT that file won't show though. Is there a way to make it work?
    – Dora
    Feb 15, 2013 at 6:26
  • Also the $hasTxt worked fine thanks thanks but I realized if I do that and let's say eh....my dropbox will just be empty then with the false it'll show let's say a message "no txt" is there a way that in the same page if there's no .txt file in the folder the whole page will just say "no txt" instead of showing the dropbox too?
    – Dora
    Feb 15, 2013 at 6:28
  • @user1850712, you're right about 'no txt', I edited the answer accordingly Feb 15, 2013 at 11:44
  • As for TxT and TXT, strtolower() is there precisely to avoid the very problem you have, so I can think about two explanations: 1) you accidentally put a space after the extension (can happen on graphic interfaces and even on the command line), or 2) you have an input method for a language written in Cyrillic, and you typed the Cyrillic equivalent of a Latin character in the extension (very unlikely though, but that's used in some kind of hacks) Feb 15, 2013 at 11:48
  • 1
    Unless you're sure your script will always run on a Windows server, most likely you want asdf.TXT to appear like that. In that case you can put this line at the beginning of the foreach: $list = strtolower($list); As for the place where my suggestions should go, your edited question has it correctly Feb 15, 2013 at 13:37
1

You could use PHP's substr() function to test the filenames:

if(substr($filename, -3) == 'txt') {
  // show file
}

See here: http://php.net/manual/en/function.substr.php

1

Try this , Hope it will work you

<?php
    if(!(is_dir("./aaa")))
    {
        die("Must create a folder first, sorry");
    }

    $lists = scandir("./aaa");
    $i =0;
    foreach($lists as $list)
    {
        if (strstr($list, '.txt')) {
        $i++;
        }
    }
    if($i == 0){
        die("the folder does not have any .txt extension files");
    }

    echo "<form action=\"./page2.php\" method=\"get\">";
    echo "<select>";

    foreach($lists as $list)
    {
        if (strstr($list, '.txt')) {
            echo "<option value=\"".substr($list,0, -4)."\">".substr($list, 0,-4)."    </option>";
        }
    }
    echo "</select>";
    echo "</form>";
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.