List Files In A Certain Folder, Strip Extensions
Moderator: General Moderators
List Files In A Certain Folder, Strip Extensions
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
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
Show this to your girlfriend, it'll get her pants wet.
Oh, and if you have a folder with a period in it 3rd or 4th from the end, you should probably not do that.
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);
?>
Re: List Files In A Certain Folder, Strip Extensions
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>
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
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>';Re: List Files In A Certain Folder, Strip Extensions
Everything works except the filename (ex. Google) is not capitalized. Here's the code I have now:
How can I capitalize the first letter of the filename?
Cheers,
Alex
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);
?>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
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:
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.
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>';
}
}Good luck, sorry it took me 3 tries lol.
Re: List Files In A Certain Folder, Strip Extensions
That's OK. Can you post the final version of the code, please?
Cheers,
Alex
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
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);
?>Re: List Files In A Certain Folder, Strip Extensions
This works exactly how I wanted it. Thanks so much!
Cheers,
Alex
Cheers,
Alex