Page 1 of 1
List Files In A Certain Folder, Strip Extensions
Posted: Sat Jul 04, 2009 3:16 pm
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
Re: List Files In A Certain Folder, Strip Extensions
Posted: Sat Jul 04, 2009 4:42 pm
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.
Re: List Files In A Certain Folder, Strip Extensions
Posted: Sat Jul 04, 2009 4:51 pm
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>
Re: List Files In A Certain Folder, Strip Extensions
Posted: Sat Jul 04, 2009 5:05 pm
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>';
Re: List Files In A Certain Folder, Strip Extensions
Posted: Sat Jul 04, 2009 6:21 pm
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
Re: List Files In A Certain Folder, Strip Extensions
Posted: Sat Jul 04, 2009 9:52 pm
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.
Re: List Files In A Certain Folder, Strip Extensions
Posted: Sat Jul 04, 2009 10:17 pm
by azylka
That's OK. Can you post the final version of the code, please?
Cheers,
Alex
Re: List Files In A Certain Folder, Strip Extensions
Posted: Sat Jul 04, 2009 11:11 pm
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!
Re: List Files In A Certain Folder, Strip Extensions
Posted: Sun Jul 05, 2009 11:11 am
by azylka
This works exactly how I wanted it. Thanks so much!
Cheers,
Alex