I have the following in my codes: <?php include("./apple.php"); ?> The page will open nicely on only certain pages. I believe it could be due to the path of apple.php, so i tried various combination but all does not work <?php include("http://www.example.com/apple.php"); ?> <?php include("/var/www/username/apple.php"); ?> <?php include("/home/username/public_html/apple.php"); ?> What should I do to correct this issue?
If apple.php is in the same directory as the script, try: include("apple.php"). If you put a . or a / before the filename, you're telling the script that the file is in the root directory (which it is not). If you put two dots before the filename, you're telling the script that the file is one directory behind the script that you're running. So, if you open a script at /public_html/abc/script.php, you'd have to do include("../apple.php") to include apple.php.
in linux, a ./ before the filename indicates that the file is in the current directory, ie the same directory as the calling script Other than that, Sevbyis correct in stating that you need a ../ for every level above you want to look. So, if the calling script is in /dir1/dir2/dir3/ and the included file is in the root of the website (/) then you'll use include('../../../apple.php');
That definitely will not work. Use the following and you'll be fine unless your host has some obscure php config settings.: <?php include($_SERVER['DOCUMENT_ROOT'].'/apple.php'); ?> Code (markup): If you ever wonder where you're at when you're working with php or would like some insight into how your host has php configured regarding paths, here's a little script I wrote a while back that I used to collect data on how about 30 different hosts handle paths. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP: Where am I? - http://greg-j.com</title> <style type="text/css"> dl{ margin:2em 0; padding:0; font-family:"Courier New", Courier, monospace; font-size:13px; overflow:auto; } dt{ margin:0; padding:.5em; width:20em; float:left; clear:both; font-weight: bold; border-top: 1px solid #999; } dd{ margin:0; padding:.5em; float:left; width:40em; white-space:pre; border-top: 1px solid #999; } </style> </head> <body> <dl> <dt>__FILE__</dt> <dd><?=__FILE__?></dd> <dt>getcwd()</dt> <dd><?=getcwd()?></dd> <dt>realpath('./')</dt> <dd><?=realpath('./')?></dd> <dt>basename(__FILE__)</dt> <dd><?=basename(__FILE__)?></dd> <dt>dirname(__FILE__)</dt> <dd><?=dirname(__FILE__)?></dd> <dt>pathinfo(__FILE__)</dt> <dd><?php print_r(pathinfo(__FILE__))?></dd> <dt>$_SERVER['REMOTE_ADDR']</dt> <dd><?=$_SERVER['REMOTE_ADDR']?></dd> <dt>$_SERVER['HTTP_HOST']</dt> <dd><?=$_SERVER['HTTP_HOST']?></dd> <dt>$_SERVER['SERVER_NAME']</dt> <dd><?=$_SERVER['SERVER_NAME']?></dd> <dt>$_SERVER['DOCUMENT_ROOT']</dt> <dd><?=$_SERVER['DOCUMENT_ROOT']?></dd> <dt>$_SERVER['SCRIPT_FILENAME']</dt> <dd><?=$_SERVER['SCRIPT_FILENAME']?></dd> <dt>$_SERVER['PHP_SELF']</dt> <dd><?=$_SERVER['PHP_SELF']?></dd> <dt>$_SERVER['REQUEST_URI']</dt> <dd><?=$_SERVER['REQUEST_URI']?></dd> <dt>$_SERVER['SCRIPT_NAME']</dt> <dd><?=$_SERVER['SCRIPT_NAME']?></dd> </dl> </body> </html> HTML: I've also attached the file to this post.
Please do not tell me your actually a webmaster... eeeek. I would head over to php.net and read a bit before just snipping someones code. You should really understand what your doing. It will help down the road. http://us.php.net/include/