Support » Everything else WordPress » Check if a folder/directory exists then display html1, if not display html2 (PHP

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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.

    catacaustic

    (@catacaustic)

    very awesome

    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.

    All I’m getting is – Folder: ”

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

    thanks

    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 ?

    catacaustic

    (@catacaustic)

    very awesome

    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";

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

    catacaustic

    (@catacaustic)

    very awesome

    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.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Check if a folder/directory exists then display html1, if not display html2 (PHP’ is closed to new replies.