vivekanand gangesh
vivekanand gangesh

Reputation: 99

how to i get only .txt formate file list from folder using php, codeigniter

I want to only .txt format files are display but all files are displaying on screen

controller code is

$this->load->helper('directory');  
$data['files'] = directory_map('./software_files');  

and view side code is

if(isset($files) && count($files) > 0 ){  
    foreach($files as $file){  
        echo $file."<br/>";  
    }  
}

Upvotes: 1

Views: 491

Answers (3)

Pradeep
Pradeep

Reputation: 9717

Hope this will help you :

You can use pathinfo() with PATHINFO_EXTENSION to check the extension for file :

foreach ($files as $file) 
{

    if (pathinfo($file, PATHINFO_EXTENSION) === 'txt')
    {
        //echo $file;
        $data[] = $file;
    }
}

print_r($data);
/*list all .txt file*/

for more : http://php.net/manual/en/function.pathinfo.php

Upvotes: 1

Hassaan
Hassaan

Reputation: 7662

Use glob to find filenames matching a pattern. Add *.txt in path to get specific type files list from directory.

$files = array();
foreach( glob("./software_files/*.txt") as $file )
{
    echo $file."<br/>";
}

UPDATE

If you want to print filename only even without file extension, Use the following line

echo basename($file,".txt")."<br/>";

Instead of

echo $file."<br/>";

Upvotes: 1

Nageen
Nageen

Reputation: 1759

You can try this

$directory = "mytheme/images/myimages";
$images = glob($directory . "*.txt");

foreach($images as $image)
{
  echo $image;
}

Upvotes: 0

Related Questions