PHP include external file!!

gaptrast

Member
Messages
123
Reaction score
0
Points
16
Hello I am using php include on my site to save both time and work.

My website:

file1.php
index.php
file2.php
/exempl/index.php
/exempl/super.php
/exempl/super2.php

How can I use the php include to include file1.php and file2.php in /exempl/index.php???

I have tried this:

Code:
<?php

include 'http://www.example.com/file1.php';

?>
BUT IT DO NOT WORK

I DESPERATELY NEED HELP QUICK!!!
 

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Including from a remote URL is not allowed, even if that URL is actually local. The simplest way is to use either relative or absolute paths to the files:
Code:
//This is relative, '../' means the parent directory
include('../file1.php');
Code:
//This is absolute, '/' means the base directory
include('/file1.php');
//If the base is not setup to the document root, it may actually be:
include('/home/YOURCPANELUSERNAME/public_html/file1.php');
Both of these will work in the '/exempl/index.php' file.
 
Last edited:

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
http://uk.php.net/manual/en/function.include.php
http://uk.php.net/manual/en/function.require.php

The only difference between require() and include() is that if an include fails, it errors. If a require fails, it freaks out and stops the page loading (in a good way). Therefore, you should use require() for includes files.

You can also use require_once() and include_once(). These are pretty much the same, but it will only include or require the file once, even if you tell it to require it twice. I can't actually see the point in _once, but it's good to know about.

~Callum
 

gaptrast

Member
Messages
123
Reaction score
0
Points
16
Thank you very very veyr much!!!!!!!!
 
Last edited:

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
You can also use require_once() and include_once(). These are pretty much the same, but it will only include or require the file once, even if you tell it to require it twice. I can't actually see the point in _once, but it's good to know about.

This can be useful when you have a complex system that is set to include or require a class's file each time it must be used. If you used the same class over and over, and it's a quite big file, it can slow down the script processing. However, if you called a _once method, the class will be loaded in the main script only once, saving some considerable overhead.
 

AngusThermopyle

Active Member
Messages
319
Reaction score
52
Points
28
I can't actually see the point in _once, but it's good to know about.

If you require or include a file, that file actually runs. ie, if you include foo.php and foo.php opens a log file, database connection, etc, then include-ing it twice will repeat the action, often not what you want.

Also, if foo.php defines a function, you will get a nasty little warning message like:

Fatal error: Cannot redeclare foobar() (previously declared in /home/zeke/public_html/foo.php:6) in /home/zeke/public_html/foo.php on line 7
 

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
It's better to not rely on _once - the code should be fixed so that it doesn't include the file more than once.

~Callum
 

AngusThermopyle

Active Member
Messages
319
Reaction score
52
Points
28
It's better to not rely on _once - the code should be fixed so that it doesn't include the file more than once.

~Callum

In some cases it is just not feasible. You include B.php and C.php which both include A.php.
And if you have several conditional requires, using require_once is simpler and less error prone than manually keeping track of which files have already been called.
 

sunnyshane9230

New Member
Messages
2
Reaction score
0
Points
0
Code:
<?php
include('exempl/index.php');
include('exempl/super.php');
?>

I think you should code like that.Because some servers were configured to process your code like "../somefile.php" or "/somefile.php" to "root/somefile.php" but another think "/somefile.php" is the peering file
 

gaptrast

Member
Messages
123
Reaction score
0
Points
16
Well it did not work....


the file 'example.com/fold/lodf/index.php' is like this:

PHP:
<?php

include('/includes/logospace.php');

include('../includes/logospace.php');

include('/home/gaptrast/public_html/includes/logospace.php');


?>

As you can see I have tried all methods in one document.

All who shows up is this:::



Warning: include(/includes/logospace.php) [function.include]: failed to open stream: No such file or directory in /home/gaptrast/public_html/pranks/test/index.php on line 3

Warning: include() [function.include]: Failed opening '/includes/logospace.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/gaptrast/public_html/pranks/test/index.php on line 3

Warning: include(../includes/logospace.php) [function.include]: failed to open stream: No such file or directory in /home/gaptrast/public_html/pranks/test/index.php on line 4

Warning: include(../includes/logospace.php) [function.include]: failed to open stream: No such file or directory in /home/gaptrast/public_html/pranks/test/index.php on line 4

Warning: include() [function.include]: Failed opening '../includes/logospace.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/gaptrast/public_html/pranks/test/index.php on line 4



Please help
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Remove the first two.

When you write a file path like:

/includes/logospace.php

the leading '/' says you are starting at the root of the file system, not at the document root of your website. So it will fail.

When you write the file path as

../includes/logospace.php

it says 'go up one directory level' and then go down into the includes directory and get logospace.php

Your script is in

/home/gaptrast/public_html/pranks/test/

up one directory level is


/home/gaptrast/public_html/pranks

which does not contain your includes directory. So the second include fails. Relative paths are fine as long as you do them right and do not change your directory structure.

The third works. Notice no error message for that line.
 
Last edited:

callumacrae

not alex mac
Community Support
Messages
5,257
Reaction score
97
Points
48
/ - root
./ - current directory
../ - up one directory

~Callum
 
Top