Forum Moderators: coopster

Message Too Old, No Replies

Help! I need to include a php file from another directory

         

buckmajor

3:57 pm on Mar 22, 2009 (gmt 0)

10+ Year Member



Hi there

I have been searching around for days and have been trying to use the php include which I am familiar with but have found no solution to my problem.
I have 2 links here
'My Web link'
http://example.com/workLogo.php

'My Photo link'
http://www.example.com/index.php

I want to add a php include from 'My Photo link' to 'My Web link' within the body content. I also have 'My Photo link' in a directory located on 'My Web link' called 'Logogallery' but can not get it to work which is the reason why I uploaded the files to a subdomain 'My Photo link'.

This is not easy for me to explain but do forgive me if I have not made myself clear enough for anyone to understand.

Would surely appreciate your help.
CHEERS :)

[edited by: eelixduppy at 5:03 pm (utc) on Mar. 22, 2009]
[edit reason] switched to example.com [/edit]

penders

6:20 pm on Mar 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



http://example.com/workLogo.php
[example.com[...]

Do these domains point to the same folder? Or are they in fact different? (They should really be the same in this instance?)

I want to add a php include from 'My Photo link' to 'My Web link' within the body content.

If it is the same folder then it should just be a case of doing something like:

[b]index.php
<body>
<?php include('workLogo.php'); ?>
</body>

I also have 'My Photo link' in a directory located on 'My Web link' called 'Logogallery' but can not get it to work which is the reason why I uploaded the files to a subdomain 'My Photo link'.

I'm not sure why you need a subdomain here (ie. [subdomain.example.com...] - but may be I'm missing something, or something has been lost is exemplification? For your 2nd 'My Photo link' page on 'My Web link' it looks as though you need to

include('../workLogo.php');
- but is this supposed to be the exact same page as your other 'My Photo link' page?!

The subdomain is often just implemented (server side) as another folder. Is this the case for you or is it more complex? Although to be honest I'm struggling as to why you would need a subdomain if it is all 1 site?

You might need to explain your directory structure a bit more (but don't use your actual domain name).

If you are wanting to include the same file from files at different levels in your directory structure it might be easier to use absolute (server) paths with the help of the $_SERVER['DOCUMENT_ROOT'] superglobal (which contains the server path to your webroot folder - where all your public website files go).

Then you can do something like the following from whatever page/file needs to include workLogo.php (regardless of what level that page/file is located in the directory tree)...

include($_SERVER['DOCUMENT_ROOT'].'/workLogo.php');

buckmajor

7:28 am on Mar 23, 2009 (gmt 0)

10+ Year Member



Hi Penders

Thanks for your reply. I tried that script and still didn't work. Maybe I'm not doing it right.
I'm trying to include from a sub directory in the same parent folder e.g. Example/Logogallery/index.php to Example/workLogo.php with this include <?php include 'Logogallery/index.php'; ?> but because I've been trying for days and no luck, so I thought it work if I uploaded the sub directory(Logogallery) to a sub domain link and try include it from there using this; <?php include 'http://www.example.com/index.php'; ?> but still no luck.

I would prefer to include from a sub directory in the same parent folder e.g. Example/Logogallery/index.php to Example/workLogo.php but I'm willing to use whatever works.

workLogo.php shows this on source view;
<div id="logo">
<?php include($_SERVER['DOCUMENT_ROOT'].'/Logogallery/index.php'); ?>
</div>

I wonder if it had something to do with the Logogallery directory?
Inside that folder is 'e2 photogallery' which holds a lot of php files and folders. I think its not including every other files in that root folder?

penders

8:42 am on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm trying to include from a sub directory in the same parent folder e.g. Example/Logogallery/index.php to Example/workLogo.php with this include <?php include 'Logogallery/index.php'; ?>

You should be able to do:

<?php include '../index.php'; ?>
in this instance (using a relative address and moving up a directory).

But hang on, I thought you said you are trying to "include from index.php to workLogo.php"? ie. You are wanting to include workLogo.php in index.php?! So that should be

<?php include '../workLogo.php'; ?>
? Please clarify.

(You should not use a subdomain to resolve this issue. That is likely to only complicate matters.)

workLogo.php shows this on source view;
<div id="logo">
<?php include($_SERVER['DOCUMENT_ROOT'].'/Logogallery/index.php'); ?>
</div>

Is this when you view the source of the page in the browser?! You should not be seeing any PHP source code when you view the source of the page in the browser! PHP is run on the server - you should only see the resulting HTML page in the browser. If this is the case then your page is not being parsed for PHP at the server end and this is probably your problem. (Or are you referring to 'source view' in your code editor?!)

But if you say the file is "Example/Logogallery/index.php", then this will need to be:

include($_SERVER['DOCUMENT_ROOT'].'/Example/Logogallery/index.php');

?

Sorry, I'm a bit confused as to which files you are trying to include. Could you outline your directory structure?

buckmajor

1:54 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



Thanks Penders

My example is the correct way; www.example.com/Logogallery/index.php to www.example.com/workLogo.php...(this looks better now)

I was referring to source view of a page in the browser which I thought php is not suppose to show but it is.

I also tried that script and no luck at all. Here is the source view of the page on a browser and again PHP is showing?

<div id="logo">
<?php include($_SERVER['DOCUMENT_ROOT'].'http://www.example.com/Logogallery/index.php'); ?>
</div>

However, I think your right about the page is not being parsed for PHP at the server end and probably the problem is with the web host company I'm with, could that be the reason why? Or is it that the link can not be included for some reason?

My friend I certainly don't want to give up but surely appreciate the time and effort you have given in helping me out.

So what I do?

penders

2:12 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



However, I think your right about the page is not being parsed for PHP at the server end and probably the problem is with the web host company I'm with, could that be the reason why? Or is it that the link can not be included for some reason?

Yes, the problem appears to be with your webhosting (company). If you already have the .php file extension on your files (which you seem to have) then any code within PHP tags inside those files should be parsed/executed. Even if there was any error with the PHP itself (such as the 'link can not be included') then you should not see any PHP code in the browser.

To check for PHP on your web server, create a very simple phpinfo page and navigate to this page in your browser:

Save the following as phpinfo.php

<?php phpinfo(); ?>

This should generate a lot of information about your installation of PHP, or nothing if PHP is not installed! (NB: Don't leave this file on a live server)

buckmajor

2:31 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



I did up phpinfo.php file and uploaded to the server but then all this information is showing with the php version etc..but what do I look at?

penders

2:40 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



all this information is showing with the php version etc..but what do I look at?

In that case PHP is installed and working on your server! The problem appears to be with your code! Back to the drawing board... hhhmmmm....

buckmajor

3:17 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



Yea it is, its the paths in the 'Logogallery' directory. All the JS and CSS are not linked so does that mean I have to alter all of them? Cause there is a lot of files to and plus there was some improvement to the page where the box was appearing when before nothing was showing.

buckmajor

4:01 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



Ok..I see whats causing the problem, when I use the normal .php file then it works without any dramas but when I use .tpl file with workLogo.php linked to that file, then it shows the php script in view source of the page in the browser for some reason.
But I'm not sure why it's not working when I'm using a php template .tpl file and workLogo.php? I used the same php script but no luck at all too :(.

penders

5:08 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



AFAIK .tpl files will not normally be parsed by PHP directly. (?) However, depending on how your template engine is implemented, your .tpl file could be included into the PHP code block within a PHP file - then it would be parsed - but may be this is not the way your template engine works?

buckmajor

1:51 am on Mar 24, 2009 (gmt 0)

10+ Year Member



Yea I think the .tpl file does not parsed PHP directly, well I should done my homework when I got this template online lol. The .tpl file works for PHP but maybe not for the include script.

This is what the code looks like when I save it to .php
workLogo.php
<?php
$title = 'Title';
$javascript = '';
$content = '<!-- HTML Code here -->';
print preg_replace("/\{([^\{]{1,100}? \}/e","$$1",file_get_contents("template.tpl"));

Thanks heaps for your help though Penders, so sorry for the hassle I should of said from the start I was using .tpl file but I didn't think the .tpl would of caused it. I appreciate your patience by resolving the issue for me. Now I can definitely use an alternative route by having workLogo.php on its own and separate from the .tpl file.

I tested it out by using the original script you gave and it works fine;
workLogo.php

<?php include("../Logogallery/index.php"); ?>

I just have to adjust the css file to align it up but it works.

Thanks heaps again Penders, if there is anything I can learn more from this then I am willing to hear it out.

CHEERS :)