0

I am trying to include a header/footer with php's include_once. I did not have any trouble until I created a sub-folder called, "states". I tried to include include_once("header.php"); and it is failing. I know it is because it is not in the same directory, so I tried include_once("/header.php"); and that did not work. I also tried doing this:

include_once("header/states.php");

include_once("header/.php");

What am I doing wrong for this to not get to the correct directory? I am trying to organize a bunch of files and I want to do this the correct way, so any help appreciated.

enter image description here

enter image description here

Becky
  • 2,283
  • 2
  • 23
  • 50

1 Answers1

1

Use ./ to reference the current directory:

        include_once("./states/header.php");

That's assuming the file you are referencing from is in the directory shown. If it's in another directory ( like _notes ) then the directory one level up can be referenced by ../, thus:

        include_once("../states/header.php");
Hektor
  • 1,845
  • 15
  • 19
  • `Warning: include_once(./states/header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\sprayfoam\SFEI\states\Alabama-Spray-Foam-Insulation.php on line 70` I got the same error with your other way before you edited it. – Becky Mar 19 '16 at 07:42
  • What is the exact path of your index.html file/the file containing the include_once(..) line? – Hektor Mar 19 '16 at 07:45
  • This is in Xampp right now, but will be on a cpanel. I just tried `` and it did not help. – Becky Mar 19 '16 at 07:47
  • I am a bit confused about this ... What is the exact path of your index.html file/the file containing the include_once(..) line? – Becky Mar 19 '16 at 07:47
  • Sorry I was wrong about the backslashes, they're understood as forward slashes on all systems. Try the path you just used with trusty old forward slashes. – Hektor Mar 19 '16 at 07:48
  • Are the two files in the same directory? – Hektor Mar 19 '16 at 07:49
  • I am trying to include this into all my files, but the one I am having troubles with is with a sub folder. So it would be C:\xampp\htdocs\sprayfoam\SFEI\states\Alabama-Spray-Foam-Insulation.php – Becky Mar 19 '16 at 07:53
  • If the header.php file is at C:\xampp\htdocs\sprayfoam\SFEI\states then you need to go two levels up the file hierarchy, which would be include_once('../../states/header.php') – Hektor Mar 19 '16 at 07:55
  • header.php is within the SFEI part. – Becky Mar 19 '16 at 07:56
  • Sorry, I thought you meant the file I was trying to include it within. – Becky Mar 19 '16 at 07:56
  • No . `Warning: include_once(../../states/header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\sprayfoam\SFEI\states\Alabama-Spray-Foam-Insulation.php on line 70` – Becky Mar 19 '16 at 08:00
  • 1
    It should be include_once('../../header.php') if the file is in the SFEI directory... – Hektor Mar 19 '16 at 08:04
  • I think that did it! So, how did you know to put the `../../` the amount of times you did? I have other files within this that still aren't working and it would be nice to know. – Becky Mar 19 '16 at 08:06
  • If a file is in the same directory, it's ./filename.php. If it's in a directory one level higher, i.e. the parent directory itself, it's ../filename.php. If it's in another directory which is in the parent directory, it's ../someOtherFolder/header.php...have a look at http://stackoverflow.com/questions/4202175/php-script-to-loop-through-all-of-the-files-in-a-directory – Hektor Mar 19 '16 at 08:09
  • So to get within the layout folder and then a file within it, from the same Alabama file, would it be `href="../layout/styles/layout.css"` – Becky Mar 19 '16 at 08:12
  • Yes, if the layout folder is located in your parent folder. – Hektor Mar 19 '16 at 08:15
  • Thanks for all of your help! – Becky Mar 19 '16 at 08:18