[Solved] How to include a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

[Solved] How to include a file

Post by Ignatius881 »

Hello.

I have some problems with de include/require_once function. For example, I've a folder called "root", and there is a subfolder in it called "test". In "root" there is a file, main.php, and in "test" there are many files which include main.php. They have this:

Code: Select all

<? include("../main.php"); ?>
But it doesn't work, and I don't know why. How can I include a file which is in a higher folder?

Thanks.
Last edited by Ignatius881 on Sat Aug 06, 2011 6:02 am, edited 1 time in total.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: How to include a file

Post by Dodon »

if these exist:

/main.php
/test/page.php

and in page.php you place the include like you did above it should work.

Do you get an error?
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

Re: How to include a file

Post by Ignatius881 »

No, there isn't an error.

It's rilated to gettext. Here is page.php:

Code: Select all

<?php require_once("../local.php"); ?>
<?php
echo _("Hello world");
?>
And here is main.php:

Code: Select all

<?php
$locale = "es_ES.utf8";
if (isSet($_GET["locale"])) $locale = $_GET["locale"];
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("messages", "./locale");
textdomain("messages");
?>
So, if I open page.php with my browser, it should show the text in Spanish, and by writing ?locale=en_GB, it's in original version. This works if both files are in the same folder, but not when they are in different folders, because it only shows the original version, not the translated one.

Of course, there is also the "locale" folder, with .mo and .po, etc.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to include a file

Post by social_experiment »

Can you show what the complete directory setup looks like?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

Re: How to include a file

Post by Ignatius881 »

social_experiment wrote:Can you show what the complete directory setup looks like?
/main.php
/locale/es_ES/LC_MESSAGES/messages.mo
/locale/es_ES/LC_MESSAGES/messages.po
/test/page.php

And page.php with <? require_once("../main.php"); ?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to include a file

Post by social_experiment »

Code: Select all

#try
<?php require_once("/../main.php"); ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

Re: How to include a file

Post by Ignatius881 »

social_experiment wrote:

Code: Select all

#try
<?php require_once("/../main.php"); ?>
It shows an empty page, with no text.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to include a file

Post by social_experiment »

Odd that require_once('../main.php') isn't working and even stranger that no error is reported. This (to me) indicates that there isn't a problem with the including of the file but something inside the file isn't working correctly.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

Re: How to include a file

Post by Ignatius881 »

I think that everything is correct and should work correctly. I don't know.

Why does it not work if both files are separated, and does it work if both files are together?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to include a file

Post by social_experiment »

You might have to contact the person/s who created the code and inform them of the problem. It's possible that other people have had / are having similar problems. Maybe they have the solution in an FAQ area.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Ignatius881
Forum Newbie
Posts: 17
Joined: Thu Jul 14, 2011 9:09 am
Location: Oviedo (Spain)

Re: How to include a file

Post by Ignatius881 »

social_experiment wrote:You might have to contact the person/s who created the code and inform them of the problem. It's possible that other people have had / are having similar problems. Maybe they have the solution in an FAQ area.
In fact, I'm helping him with that project, we're friends :lol:

But it doesn't matter, I've found a solution thanks to an IRC channel. Thanks anyway for your help :D
Post Reply