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
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.

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.