WordPress.org

Ready to get started?Download WordPress

Forums

Check if a folder/directory exists then display html1, if not display html2 (PHP (8 posts)

  1. luxman
    Member
    Posted 1 year ago #

    I am trying to add custom html to a 404 page in wordpress.

    <?php
    $filename = "/find";
    if (!file_exists($filename))
    echo $filename, " display html2 ";
    elseif (!is_dir($filename))
    echo $filename, " display html1 ";
    ?>

    If someone visits http://www.demosite.com/about/whatever = Display HTML 1

    If someone visits http://www.demosite.com/find/whatever = Display HTML 2

    Is this even possible with PHP and HTML?

  2. luxman
    Member
    Posted 1 year ago #

    I'm trying:

    <?php
        $filename = "/find";
        if (is_dir($filename)){
        echo "<h1>hello</h1>";
        }
        else{
        echo "<h1>nope</h1>";
        }
        ?>

    But it doesn't work. The word "nope" appears on both:

    http://www.demosite.com/about/whatever

    http://www.demosite.com/find/whatever

    I'd like to have "hello" appear under the /find/whatever pages.

  3. catacaustic
    Member
    Posted 1 year ago #

    If you're getting "nope" all the time, then your script can't find that folder. The main issues that I've had with this before is when the directory structure is not how I expect it to be. An an example from yoru own code, you're asking for the folder '/find'. Because you're putitng the '/' in front of the folder name, your system is looking in the root folder for the file system, and I have a feeling that you're trying to look for that folder relative to the file that you're currently using.

    As a simlpe de-bugging idea, try adding this before your if() statement

    echo "<p>Folder: '".realpath ("/find")."'</p>";

    Check the output of that because it will tell you the exact folder on the file system that it's searching for. If the echo doesn't return anythin, then you're requesting in invalid folder URI.

  4. luxman
    Member
    Posted 1 year ago #

    All I'm getting is - Folder: ''

    so I guess I have the wrong path? How would I go about finding the right one?

    thanks

  5. luxman
    Member
    Posted 1 year ago #

    I tried echo getcwd(); and I got - /home/user/public_html/demosite.com

    so that means the 404.php is on the root domain, even though i'm viewing http://www.demosite.com/find/whatever ?

  6. catacaustic
    Member
    Posted 1 year ago #

    No. What is happening is that PHP works on the relative file position of the main file that's being called. In the case of WordPress's public-facing area, this is always index.php in hte webistes root public folder (or sub-directory if you've installed in a sub-directory) so you will always be working in that directory.

    The way to get around this is to get used ot using complete file/folder URI's for everything. The sooner that you can cut out relative URI's the eaiser your life will be!

    If you need the directory that your actual file is in, you can use:

    dirname (__FILE__)

    Then add whatever folders you need after that:

    $dir = dirname (__FILE__)."/find";

  7. luxman
    Member
    Posted 1 year ago #

    I'm not looking for a file though... just the directory.

  8. catacaustic
    Member
    Posted 1 year ago #

    The example that I've given you is looking for a directory. The PHP function file_exists() looks for both files and folders and doesn't differentiate between the two, all it cares about is that whatever you have asked for exists.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.