Reputation: 99
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
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
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/>";
}
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
Reputation: 1759
You can try this
$directory = "mytheme/images/myimages";
$images = glob($directory . "*.txt");
foreach($images as $image)
{
echo $image;
}
Upvotes: 0