1

Hi I don't know which path should put into parameter in require_once()

I am working with 7.2.10-0.

I have a directory which is

ice_hockey > data > teams.php 
ice_hockey > view > top.php
ice_hockey > index.php

My teams.php is like this

<?php
require_once('../view/top.php');
?>

but It does not work. I was searching for the path, especially relative path to put as parameter in require_once. Whatever I did, it doesn't work..

If I am put teams.php in the ice_hockey folder.
It will be easy but I need that folder to make easy.

How can I do? What is my problem here?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
sk Jin
  • 33
  • 5

1 Answers1

1

The current working directory is not guaranteed to be where the script is located. When you access index.php from the web server, your CWD is going to be ice_hockey, not ice_hockey/data.

The same would apply if you were running in the command line from the root directory and ran:

php ice_hockey/data/teams.php

The CWD would be the root directory, or where you executed the command from.

Use the magic constant __DIR__ to use the current script's directory.

require_once(__DIR__ . '/../view/top.php');

This will always point to the correct location relative to where teams.php is located.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95