0

I have written config.php, head.php in my public_html, now i need them in the new folder (public_html/test/gallery) where test is a sub-domain using require and include respectively.

<?php
require_once"../config.php";
require_once"../func.php";
include_once"../head.php";
echo "are u mad";
?>

It keeps returning a blank page. How do I do that?

sjagr
  • 15,983
  • 5
  • 40
  • 67
  • 6
    You can put paths into the include directives... `include('../whatever.php')` – Marc B Jan 05 '15 at 21:43
  • Read up on include and require in the php manual? How about that? – Rizier123 Jan 05 '15 at 21:44
  • try: `require_once"../../config.php"` Does that do the trick for you? – Rizier123 Jan 05 '15 at 21:49
  • Does it have something to do with the missing `( )` around `../config.php`? **Note** I only ask because I have only used `require()` with the brackets. – Tim Lewis Jan 05 '15 at 21:51
  • 2
    @TimLewis No it does not, it's the same with echo, you could do `echo ("xy");` but you also can do `echo "xy";` (Also see here: http://php.net/manual/en/function.echo.php) – Rizier123 Jan 05 '15 at 21:53
  • possible duplicate of [PHP - with require\_once/include/require, the path is relative to what?](http://stackoverflow.com/questions/1954750/php-with-require-once-include-require-the-path-is-relative-to-what) – worldofjr Jan 05 '15 at 21:54
  • in which page you are calling the above and where is it located in your site? – Aram Tchekrekjian Jan 05 '15 at 21:55
  • Warning: require(../../config.php): failed to open stream: No such file or directory in /home/peaksmso/public_html/test/gallery/gallery.php on line 12 Warning: require(../../config.php): failed to open stream: No such file or directory in /home/peaksmso/public_html/test/gallery/gallery.php on line 12 Fatal error: require(): Failed opening required '../../config.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/peaksmso/public_html/test/gallery/gallery.php on line 12 – Oluwatayo Adeyemi Jan 05 '15 at 21:55
  • @ Aram Tchekrekjian, it is located in /public_html/test i.e /public_html/config.php I need it in /public_html/test/gallery/gallery.php – Oluwatayo Adeyemi Jan 05 '15 at 21:57
  • @rm-vanda, explain well please, am new in php, I don't understand You quite well – Oluwatayo Adeyemi Jan 05 '15 at 22:01
  • Not clear, come down to my level please – Oluwatayo Adeyemi Jan 06 '15 at 00:22

2 Answers2

1

Use absolute paths always, and you'll never bang your head like this again.

include_once "/var/www/path/to/your/script.php"; 

And also, you can debug this much more effectively if you put the following at the top of your pages which you are having trouble with:

ini_set("display_errors","On");
error_reporting(-1); 
rm-vanda
  • 3,122
  • 3
  • 23
  • 34
0

I like to include all files relative to the document root like so:

include_once $_SERVER['DOCUMENT_ROOT'] . "/depends/where/code/is.php";

the good thing is that all files will be in relation to one source root. This makes your code more like fluid!

*Tip: you can also specify an include path in your php.ini config file. This helps PHP find your file faster too.

ZenStein
  • 151
  • 7