简体   繁体   中英

show a list of file with a specific extension in PHP

I have this script and is working very good, now, I need to show just a list of an extension file, like ".TXT"...

if I run this code, I will have all list of all file, but I need to show just a specific extension...

I'm new in PHP, and I try to add $file != ".TXT" but doesn't work...

Thank you very much :)

 <?php 

 $User = $_GET['User'];

   $dir    = "myfolder/$User";

        if (is_dir($dir)) { if ($dh = opendir($dir)) {

            while (($file = readdir($dh)) !== false) {                
                clearstatcache();
                if(is_file($dir."/".$file )) {    
                    echo '';             
                    echo $file;
                    echo "|";                    
                    echo "DATE:" . date ("F d, Y H:i:s", filemtime(utf8_decode($dir."/".$file)));
                    echo "|";
                }                
            }            

            //echo "<br>";
            closedir($dh);
        }
    }   
   ?>  

Use php's pathinfo() .

while (($file = readdir($dh)) !== false) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if (strtolower($ext) == '.txt') {
// PUT YOUR CODE HERE

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