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

truelux

Regular Member
Joined
Jun 16, 2011
Messages
330
Reaction score
94
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!
 
ofcourse it is possible, with what you are having a problem exactly?
 
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
 
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.
 
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.
 
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..
.htasess is not showing in many providing hosting, than what we do for that, can any one suggest to help us.
 
Instead of echo $filename do include(path/to/$filename) either relative to actually run script or direct from server's /
 
Back
Top