PHP and paths - relative to file and not including code

后端 未结 1 706
野趣味
野趣味 2021-01-29 12:30

I am really confused about PHP and the path system.

I have this structure:

- Includes
- Login
- View
   * Secure

These are all director

相关标签:
1条回答
  • 2021-01-29 12:58

    There are two types of paths: relative, and absolute. Absolute paths start with a /.. (or C:/.. or however that works on Windows) and always unequivocally point at one specific file.

    Relative paths (not starting with /) are relative to the include_path variable. That's a variable that contains a bunch of folders, where PHP will look for your relative path. That include_path also includes ., the current directory. The "current" directory depends on which file was the "first" to be executed. So it varies depending on which file starts to include others.

    1. You can set your include_path to some specific, unchanging directory, so you always have a fixed path to include to relatively.
    2. Better: construct absolute paths:

      include __DIR__ . '/../Login/file.php';
      

      This is always relative to the directory the file is in.

    0 讨论(0)
提交回复
热议问题