Jump to content

Including something not in same folder


dudejma

Recommended Posts

Is it possible to include a file that isn't in the same folder?

 

Ex:

 

include  'http://mydomain.org/file/include.php';

 

If that doesn't work, does anyone know what does? I'm trying to keep my files separated so it's not so hard to find a certain file but I need to have a way for each file to access one include. That way, I don't have to change the same thing ten times over again. Thanks!

Link to comment
https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/
Share on other sites

You don't include a url; all you'll get is whatever output is generated by the php parser. You include via a filesystem path.

yeah i didn't even look at his example... this is from php.net

 

<?php

/* This example assumes that www.example.com is configured to parse .php
* files and not .txt files. Also, 'Works' here means that the variables
* $foo and $bar are available within the included file. */

// Won't work; file.txt wasn't handled by www.example.com as PHP
include 'http://www.example.com/file.txt?foo=1&bar=2';

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';

// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';

$foo = 1;
$bar = 2;
include 'file.txt';  // Works.
include 'file.php';  // Works.

?>

That's the way it should be done, yes. For example, db_conn.php contains the connection details, and is in /var/www/html/includes/ and make_a_random_query.php resides in /var/www/html/. In make_a_random_query.php, you'd include (or preferably require_once() ) db_conn.php via the filesystem path, which can be either relative or absolute.

 

make_a_random_query.php

require_once('includes/db_conn.php'); // relative path
// OR //
require_once('/var/www/html/includes/db_conn.php'); // absolute path

I should probably mention that on a hosting account, it will almost always be easier to use a relative path. Often the absolute path will be many directories deep . . . It probably wouldn't be a bad idea to familiarize yourself with $_SERVER['DOCUMENT_ROOT'] as well.

I should probably mention that on a hosting account, it will almost always be easier to use a relative path. Often the absolute path will be many directories deep . . . It probably wouldn't be a bad idea to familiarize yourself with $_SERVER['DOCUMENT_ROOT'] as well.

 

You said that it'd be better to use a relative path, but what if I have a file in site/hub but what if I want to use a db_conn.php which is in site/include?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.