1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Check if a folder/directory exists then display html1, if not display html2

Discussion in 'PHP & Perl' started by truelux, Feb 14, 2013.

  1. truelux

    truelux Regular Member

    Joined:
    Jun 16, 2011
    Messages:
    298
    Likes Received:
    91
    I am trying to add custom html to a 404 page.


    PHP:
    <?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?



    thanks in advance!
     
  2. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,304
    Likes Received:
    1,799
    Location:
    www.Indexification.com
    Home Page:
  3. bpmik

    bpmik Newbie

    Joined:
    Feb 4, 2013
    Messages:
    45
    Likes Received:
    8
    I know it's easily done in the web server, like apache, you just tell apache which 404 page to serve, can you do that in PHP somehow?


    I would create the custom 404 page in PHP or HTML whatever you want. Then if you have an apache server, edit or create your .htaccess file or httpd.conf if server is yours. I am not a pro with server directives but according to a site I look at "How to Create Custom 404 Pages", I cannot link, its as simple as adding the line:

    ErrorDocument 404 /404.php
     
  4. nickcarter

    nickcarter Newbie

    Joined:
    Feb 19, 2013
    Messages:
    15
    Likes Received:
    3
    htacess is the solution
     
  5. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,067
    Likes Received:
    2,882
    Gender:
    Male
    Obviously it is possible. You need the full document path, which i don't think you have provided. For example:

    Code:
    <?
    $filename = "/home/public_html/find";
    if (!file_exists($filename))
        echo $filename, " display html2 ";
    elseif (!is_dir($filename))
        echo $filename, " display html1 ";
    ?>
    
    All depends on what you are trying to achieve.
     
  6. SacDin

    SacDin Newbie

    Joined:
    Feb 25, 2013
    Messages:
    4
    Likes Received:
    0
    What do you want to get exactly ?

    I mean, Do you want separate page for every section / folder ? If yes, it's easily possible using htaccess. Let me know, i will help you on this. If you want it to be directory specific (any custom dir), you will need to use PHP for that obviously.
     
  7. mseostrategy

    mseostrategy BANNED BANNED

    Joined:
    Oct 2, 2012
    Messages:
    1,368
    Likes Received:
    296
    .htasess is not showing in many providing hosting, than what we do for that, can any one suggest to help us.
     
  8. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,067
    Likes Received:
    2,882
    Gender:
    Male
    Checking if file or folder exists doesn't require htaccess atall, but yes.. If you need pretty permalinks, you need htaccess and mod_rewrite enabled..
     
  9. ventr

    ventr Newbie

    Joined:
    Jan 29, 2013
    Messages:
    28
    Likes Received:
    4
    Instead of echo $filename do include(path/to/$filename) either relative to actually run script or direct from server's /