dudejma Posted July 28, 2011 Share Posted July 28, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/ Share on other sites More sharing options...
AyKay47 Posted July 28, 2011 Share Posted July 28, 2011 sure as long as the path to the file is correct Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248778 Share on other sites More sharing options...
dudejma Posted July 28, 2011 Author Share Posted July 28, 2011 Every time I try it, it says that it can't connect to the database, but when I put the exact same file in the folder that it's in, it works. Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248780 Share on other sites More sharing options...
Pikachu2000 Posted July 28, 2011 Share Posted July 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248781 Share on other sites More sharing options...
AyKay47 Posted July 28, 2011 Share Posted July 28, 2011 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. ?> Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248782 Share on other sites More sharing options...
dudejma Posted July 28, 2011 Author Share Posted July 28, 2011 Can you explain what $foo and $bar are for? I read some things on the PHP website and I never could get what $foo and $bar are for. Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248800 Share on other sites More sharing options...
AyKay47 Posted July 29, 2011 Share Posted July 29, 2011 lol, it's just example text.. foo and bar are just the names that they choose to use for there variables, arguments etc.. Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248804 Share on other sites More sharing options...
dudejma Posted July 29, 2011 Author Share Posted July 29, 2011 Then what are the variables supposed to represent? Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248808 Share on other sites More sharing options...
AyKay47 Posted July 29, 2011 Share Posted July 29, 2011 just variables passed through the url... Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248809 Share on other sites More sharing options...
dudejma Posted July 29, 2011 Author Share Posted July 29, 2011 Okay, I know I sound stupid, but I've never dealt with stuff like this before. What's the point of them then? What do they do? Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248811 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2011 Share Posted July 29, 2011 In the above context, it's just to show that the values of the variables are available to the scripts that are include()d via the filesystem path. Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248812 Share on other sites More sharing options...
dudejma Posted July 29, 2011 Author Share Posted July 29, 2011 Ohhhhhhhhhh! I'm thinking this is to connect to a database. Not share variables. I get it now. Thanks Pikachu and AyKay! Can you connect to a database using a filesystem path? Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248814 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2011 Share Posted July 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248819 Share on other sites More sharing options...
dudejma Posted July 29, 2011 Author Share Posted July 29, 2011 Ohhh, okay. Thanks Pikachu! Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248834 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2011 Share Posted July 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248836 Share on other sites More sharing options...
dudejma Posted July 29, 2011 Author Share Posted July 29, 2011 Okay, I'll look over it. Thanks again Pikachu! Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248838 Share on other sites More sharing options...
dudejma Posted July 29, 2011 Author Share Posted July 29, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1248880 Share on other sites More sharing options...
Pikachu2000 Posted July 29, 2011 Share Posted July 29, 2011 include '../include/db_conn.php'; Quote Link to comment https://forums.phpfreaks.com/topic/243131-including-something-not-in-same-folder/#findComment-1249049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.