Printing a filename without .txt

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
conthox
Forum Commoner
Posts: 39
Joined: Tue Jun 25, 2002 1:44 pm
Location: Sweden

Printing a filename without .txt

Post by conthox »

Hello

I've got a problem,.

I have this script, it list .txt files in a folder and print it:

Code: Select all

if ($handle = opendir(".")) 
   { 
    while (false !== ($file = readdir($handle))) 
   {
   if (substr($file,-4)==".txt") echo "$file<br>"; 
   &#125; 
   closedir($handle); 
&#125;
How do I do to print the filename without the .txt ?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

the simplest, 'nothing to worry about" way is to use http://www.php.net/manual/en/function.pathinfo.php

Since you already know the extension is '.txt' and 4 characters long you may also use
echo substr($file,0,-4), '<br>';
conthox
Forum Commoner
Posts: 39
Joined: Tue Jun 25, 2002 1:44 pm
Location: Sweden

Post by conthox »

Thank you very much

Hmm,. So simple, but sometimes so difficult to figure out,... :oops:

But here is another one:

How do I sort the output, for exaple by date of upload, or a part of the filename?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

conthox
Forum Commoner
Posts: 39
Joined: Tue Jun 25, 2002 1:44 pm
Location: Sweden

Post by conthox »

Vielen dank!
(Thanks!)
Post Reply