List Files In A Certain Folder, Strip Extensions

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
azylka
Forum Commoner
Posts: 40
Joined: Sat Dec 06, 2008 9:11 pm

List Files In A Certain Folder, Strip Extensions

Post by azylka »

Hi! I'm looking for a bit of code that can show the names of the files in a folder while making the first letter of the filename capitalized and stripping the last four letters (which would be the extension ".php").

Cheers,
Alex
Squibbles1077
Forum Newbie
Posts: 8
Joined: Sat Jul 04, 2009 2:00 pm
Location: US

Re: List Files In A Certain Folder, Strip Extensions

Post by Squibbles1077 »

Show this to your girlfriend, it'll get her pants wet.

Code: Select all

<?php
$path = "/home/content/your_username/subfolder/";
$dir = @opendir($path) or die("Unable to open $path");
echo 'Directory of ' . $path;
while ($file = readdir($dir)) 
{
   if (substr($file, -4, 1) == '.')
   {
       $waffles = substr($file, -1 * strlen($file), strlen($file) - 4);
       echo '<a href="' . $waffles . '">' . $waffles . '</a><br>';
   }
   if (substr($file, -5, 1) == '.')
   {
       $pancakes = substr($file, -1 * strlen($file), strlen($file) - 5);
       echo '<a href="' . $pancakes . '">' . $pancakes . '</a><br>';
   }
   if (substr($file, -5, 1) != '.' && substr($file, -4, 1) != '.'){
       echo '<a href="' . $file . '">' . $file . '</a><br>';
   }
}
closedir($dir);
?> 
 
Oh, and if you have a folder with a period in it 3rd or 4th from the end, you should probably not do that.
azylka
Forum Commoner
Posts: 40
Joined: Sat Dec 06, 2008 9:11 pm

Re: List Files In A Certain Folder, Strip Extensions

Post by azylka »

Hi. I think there was a misunderstanding. I want to strip the extension from the file being linked to.
Like:

The /links folder has google.php in it.

The code that I need is supposed to return:
<a href="links/google.php">Google</a>
Squibbles1077
Forum Newbie
Posts: 8
Joined: Sat Jul 04, 2009 2:00 pm
Location: US

Re: List Files In A Certain Folder, Strip Extensions

Post by Squibbles1077 »

Ah, I see. Just replace the 1st $pancakes and $waffles with $file, like this:

Code: Select all

echo '<a href="' . $file . '">' . $waffles . '</a><br>';
echo '<a href="' . $file . '">' . $pancakes . '</a><br>';
azylka
Forum Commoner
Posts: 40
Joined: Sat Dec 06, 2008 9:11 pm

Re: List Files In A Certain Folder, Strip Extensions

Post by azylka »

Everything works except the filename (ex. Google) is not capitalized. Here's the code I have now:

Code: Select all

<?php
$path = "links/";
$dir = @opendir($path) or die("Unable to open $path");
while ($file = readdir($dir))
{
   if (substr($file, -4, 1) == '.')
   {
       $waffles = substr($file, -1 * strlen($file), strlen($file) - 4);
       echo '<a href="' . $file . '">' . $waffles . '</a>';
   }
   if (substr($file, -5, 1) == '.')
   {
       $pancakes = substr($file, -1 * strlen($file), strlen($file) - 5);
       echo '<a href="' . $file . '">' . $pancakes . '</a>';
   }
   if (substr($file, -5, 1) != '.' && substr($file, -4, 1) != '.'){
       echo '<a href="' . $file . '">' . $file . '</a>';
   }
}
closedir($dir);
?>
How can I capitalize the first letter of the filename?

Cheers,
Alex
Squibbles1077
Forum Newbie
Posts: 8
Joined: Sat Jul 04, 2009 2:00 pm
Location: US

Re: List Files In A Certain Folder, Strip Extensions

Post by Squibbles1077 »

Oh, I guess I didn't read that part of your first post. Here's the new while() loop that will uppercase the first character of each string:

Code: Select all

while ($file = readdir($dir)) 
{
   if (substr($file, -4, 1) == '.')
   {
       $waffles = substr($file, -1 * strlen($file), strlen($file) - 4);
       echo '<a href="' . $file . '">' . ucfirst($waffles) . '</a><br>';
   }
   if (substr($file, -5, 1) == '.')
   {
       $pancakes = substr($file, -1 * strlen($file), strlen($file) - 5);
       echo '<a href="' . $file . '">' . ucfirst($pancakes) . '</a><br>';
   }
   if (substr($file, -5, 1) != '.' && substr($file, -4, 1) != '.'){
       echo '<a href="' . $file . '">' . ucfirst($file) . '</a><br>';
   }
}
all I did was enclose the variables that are being output to the screen in ucfirst().

Good luck, sorry it took me 3 tries lol.
azylka
Forum Commoner
Posts: 40
Joined: Sat Dec 06, 2008 9:11 pm

Re: List Files In A Certain Folder, Strip Extensions

Post by azylka »

That's OK. Can you post the final version of the code, please?

Cheers,
Alex
Squibbles1077
Forum Newbie
Posts: 8
Joined: Sat Jul 04, 2009 2:00 pm
Location: US

Re: List Files In A Certain Folder, Strip Extensions

Post by Squibbles1077 »

Code: Select all

 
<?php
$path = '/home/content/username/html/'; //change this
$dir = @opendir($path) or die("Unable to open $path");
echo 'Directory of ' . $path;
while ($file = readdir($dir)) 
{
   if (substr($file, -4, 1) == '.')
   {
       $waffles = substr($file, -1 * strlen($file), strlen($file) - 4);
       echo '<a href="' . $file . '">' . ucfirst($waffles) . '</a><br>';
   }
   if (substr($file, -5, 1) == '.')
   {
       $pancakes = substr($file, -1 * strlen($file), strlen($file) - 5);
       echo '<a href="' . $file . '">' . ucfirst($pancakes) . '</a><br>';
   }
   if (substr($file, -5, 1) != '.' && substr($file, -4, 1) != '.'){
      echo '<a href="' . $file . '">' . ucfirst($file) . '</a><br>';
   }
}
closedir($dir);
?>
That should be it. Good luck!
azylka
Forum Commoner
Posts: 40
Joined: Sat Dec 06, 2008 9:11 pm

Re: List Files In A Certain Folder, Strip Extensions

Post by azylka »

This works exactly how I wanted it. Thanks so much!

Cheers,
Alex
Post Reply