简体   繁体   中英

droplist only with specific file with php

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.

在此处输入图片说明


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

在此处输入图片说明

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

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.

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>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM