readdir help needed

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
ZachsCompServ
Forum Newbie
Posts: 15
Joined: Thu Aug 26, 2010 11:29 pm

readdir help needed

Post 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.
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: readdir help needed

Post 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:

Code: Select all

yabadabadoo
ZachsCompServ
Forum Newbie
Posts: 15
Joined: Thu Aug 26, 2010 11:29 pm

Re: readdir help needed

Post 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!
User avatar
MindOverBody
Forum Commoner
Posts: 96
Joined: Fri Aug 06, 2010 9:01 pm
Location: Osijek, Croatia

Re: readdir help needed

Post 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. :D
It tells you what is going on in code at first sight!

Cheers!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: readdir help needed

Post 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.
ZachsCompServ
Forum Newbie
Posts: 15
Joined: Thu Aug 26, 2010 11:29 pm

Re: readdir help needed - ONE MORE PROBLEM

Post 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"!
Last edited by ZachsCompServ on Mon Aug 30, 2010 12:25 am, edited 1 time in total.
ZachsCompServ
Forum Newbie
Posts: 15
Joined: Thu Aug 26, 2010 11:29 pm

Re: readdir help needed

Post 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!
ZachsCompServ
Forum Newbie
Posts: 15
Joined: Thu Aug 26, 2010 11:29 pm

Re: readdir help needed

Post 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
Post Reply