Page 1 of 1
readdir help needed
Posted: Thu Aug 26, 2010 11:34 pm
by ZachsCompServ
I'm designing a site for a church and wanted to make it easy for them to update it so I don't have to do a lot of updating due to me doing this for free. I have the following code:
Code: Select all
<?php
if ($handle = opendir('sermons')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href='sermons/$file'>$file\n</a><br>";
}
}
closedir($handle);
}
?>
It will echo the filename plus the extension. (file.txt). How can I put it in here to exclude ".txt" from this, so it would just echo "file"? I'm doing this for the Pastor at the church who wants to make her weekly sermons available online by date. I'd rather it just say "September 29, 2010" instead of "September 20, 2010.txt". Thanks.
Re: readdir help needed
Posted: Thu Aug 26, 2010 11:59 pm
by MindOverBody
The simplest solution is
basename function which have suffix parameter which tells to the function what file suffix will function cut off.
Example:
Code: Select all
$file = "yabadabadoo.txt";
echo basename( $file, ".txt" );
...will output:
Re: readdir help needed
Posted: Fri Aug 27, 2010 12:29 am
by ZachsCompServ
Works perfectly. I was able to find another solution that I did use until you suggested baseline. I temporarily used str_replace():
Code: Select all
<?php
if ($handle = opendir('sermons')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$echo = str_replace(".txt","",$file);
echo "<a href='sermons/$file'>$echo\n</a><br>";
}
}
closedir($handle);
}
?>
But now, I'm using baseline over str_replace. Here's what I have now that works perfectly:
Code: Select all
<?php
if ($handle = opendir('sermons')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$echo = basename($file,".txt");
echo "<a href='sermons/$file'>$echo\n</a><br>";
}
}
closedir($handle);
}
?>
Thank you so much for your help!
Re: readdir help needed
Posted: Fri Aug 27, 2010 12:49 am
by MindOverBody
No problem.
str_replace function is also respectfull solution, actualy, it is bit faster than basename function, but not much.
If you ask me, i would go with basename function. It is more elegant and it's not such eye'stabing.

It tells you what is going on in code at first sight!
Cheers!
Re: readdir help needed
Posted: Fri Aug 27, 2010 11:29 am
by Jonah Bron
You might want to point the url to another PHP page so that you can make things prettier. Like this:
Code: Select all
echo "<a href='sermons/$file'>$echo\n</a><br>";
//should be
echo '<a href="sermons.php?sermon=' . $file . '">' . $echo . '</a><br />' . PHP_EOL;
And sermons.php would look like this:
Code: Select all
<?php
$sermon_file = isset($_GET['sermon']) ? $_GET['sermon'] : null;
$sermon = empty($sermon_file) ? 'No Sermon Specified' : basename($sermon_file, '.txt');
?><!DOCTYPE html>
<html>
<head>
<title><?php echo $sermon; ?></title>
</head>
<body>
<h1><?php echo $sermon; ?></h1>
<?php
$text = file_get_contents('sermons/' . $sermon_file);
$text = '<p>' . $text . '</p>';
$text = str_replace("\n\n", '</p><p>', $text);
$text = nl2br($text);
$text = str_replace('</p></p>', "</p>\n\n</p>", $text);
echo $text;
?>
</body>
</html>
This way you can apply CSS and stuff.
Re: readdir help needed - ONE MORE PROBLEM
Posted: Mon Aug 30, 2010 12:09 am
by ZachsCompServ
One More Problem:
when an apostrophe ( ' ) is in the file name, it messes everything up. Any "shortcut" to searching the filename for an apostrophe and automatically putting in a "/" before it to make it easier?
PROBLEM SOLVED: LOOK AT POST BELOW FOR MY SOLUTION TO THIS APOSTROPHE "PROBLEM"!
Re: readdir help needed
Posted: Mon Aug 30, 2010 12:21 am
by ZachsCompServ
NEVERMIND:
I added this line into the PHP before the echo:
Code: Select all
$apostrophe = str_replace("'","'",$file);
Thanks for all of your help. I got it working now!
Re: readdir help needed
Posted: Mon Aug 30, 2010 7:55 pm
by ZachsCompServ
Jonah Bron wrote:You might want to point the url to another PHP page so that you can make things prettier. Like this:
Code: Select all
echo "<a href='sermons/$file'>$echo\n</a><br>";
//should be
echo '<a href="sermons.php?sermon=' . $file . '">' . $echo . '</a><br />' . PHP_EOL;
And sermons.php would look like this:
Code: Select all
<?php
$sermon_file = isset($_GET['sermon']) ? $_GET['sermon'] : null;
$sermon = empty($sermon_file) ? 'No Sermon Specified' : basename($sermon_file, '.txt');
?><!DOCTYPE html>
<html>
<head>
<title><?php echo $sermon; ?></title>
</head>
<body>
<h1><?php echo $sermon; ?></h1>
<?php
$text = file_get_contents('sermons/' . $sermon_file);
$text = '<p>' . $text . '</p>';
$text = str_replace("\n\n", '</p><p>', $text);
$text = nl2br($text);
$text = str_replace('</p></p>', "</p>\n\n</p>", $text);
echo $text;
?>
</body>
</html>
This way you can apply CSS and stuff.
I'm a little confused by all of this. I'm going to get started on this later this week, but my idea is to have the links on the sermons.php page to link to a new page, sermons_open.php, that will have the filename after it (i.e. sermons_open.php?file=file, 08-30-2010.txt). The only thing is that I don't think spaces can be in the web address, even if it's in this form.
I'll get to work on it later this week as I said, and if I run into any problems I'll post it in a new thread. Just trying to find this topic in my php book and/or on the web. Just don't know what topic it would be under, but I do remember seeing this somewhere. Thanks