Jump to content

[SOLVED] How to use this function to only output txt files?


black_thunder

Recommended Posts

This function is part of a news script, and its output currently shows all files in the folder, including the .htaccess file, which I do not want shown as part of the news. The script's WSIWYG editor saves the files as *.txt files, then uses this script to call them and display them.

function getNewsList(){

   $fileList = array();
   
// Open the actual directory
if ($handle = opendir("admin/news")) {
	// Read all file from the actual directory
	while ($file = readdir($handle))  {
	    if (!is_dir($file)) {
	       $fileList[] = $file;
      	}
	}
}	

rsort($fileList);

return $fileList;
}

How can I use this existing function to retrive only .txt files? :confused:

 

 

<?php
function getNewsList(){
   
   $fileList = array();
   
   $files = glob("admin/news/*.txt");
   
   foreach($files as $file) {
   
   $parts = explode("/", $file);
   $fileList[] = $parts[2];
   
   }
   
   rsort($fileList);
   return $fileList;
   
}
?>

 

That should do it for you

@Wolfrage: Sorry, but I dont see anything in there that would help me.

I looked at the PHP manual for the PHP function opendir, and the comments following, and I see code that people have made to show only files with a specific ending, but I cant figure out how to tie it in to the existing script...I dont want to break the script that relies on this function...

 

@MatthewJ: I'll try that and post results. Thanks!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.