Newbie Include problem

Discussion in 'PHP' started by nismo, May 28, 2008.

  1. #1
    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?
     
    nismo, May 28, 2008 IP
  2. nismo

    nismo Peon

    Messages:
    359
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Anyway, apple.php resides in public_html folder.
     
    nismo, May 28, 2008 IP
  3. Sevby

    Sevby Guest

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    Sevby, May 28, 2008 IP
  4. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #4
    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');
     
    relixx, May 28, 2008 IP
  5. wild13

    wild13 Guest

    Messages:
    9
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Use <?php include("/apple.php"); ?>
     
    wild13, May 28, 2008 IP
  6. Greg-J

    Greg-J I humbly return to you.

    Messages:
    1,844
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    135
    #6
    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.
     

    Attached Files:

    Greg-J, May 28, 2008 IP
  7. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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/
     
    LittleJonSupportSite, May 28, 2008 IP