31137

PHP - include() or require() with relative paths won't work on windows, even when appending __D

Question:

I was reading here about problems in PHP when using include() or required() with relative paths and all the solutions I saw was to append <strong>DIR</strong>

I'm currently working on Windows, and even though the error message displays the current value of <strong>DIR</strong>, then the relative path seems to be added as a string, rather than going one level up, for example:

include(__DIR__ . "..\\another_folder\\file_2.php");

produces the following error: Warning: include(C:\xampp\htdocs\main_folder..\another_folder\file_2.php) [function.include]: failed to open stream: No such file or directory in

Any idea what's going on?

Answer1:

You need to add the \ after the directory name:

include(__DIR__ . "\\..\\another_folder\\file_2.php");

This will make the path be

<blockquote>

C:\xampp\htdocs\main_folder\..\another_folder\file_2.php

</blockquote>

instead of

<blockquote>

C:\xampp\htdocs\main_folder..\another_folder\file_2.php

</blockquote>

Also, for portability, it is advisable to use / instead of \, which works on all platforms, including Windows:

include(__DIR__ . "/../another_folder/file_2.php");

Answer2:

Don't use backslashes in paths in PHP, use regular forward slashes (/) everywhere. PHP will translate to the appropriate OS-specific directory separators for you automatically.

That being said, look at the error message in detail:

... lude(C:\xampp\htdocs\main_folder..\another_folder\file_2.php) [func... ^--- missing a slash here

Recommend

  • Extract data from User defined type via cx_Oracle
  • Including Paragonie Halite in project doesn't find variables and functions
  • How to get PHP working in Brackets?
  • Zend\\ServiceManager\\ServiceManager::get was unable to fetch or create an instance for getAlbum
  • Xcache var_size error
  • Accessing the real file name of a symbolic linked file
  • error on mysqli::connect_errno
  • Strict Standards: Non-static method STemplate::assign() should not be called statically
  • My PHP code is commented out
  • Laravel : Integrity constraint violation
  • removing last 3 characters on a file (file extension)
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • opencv display image without x server
  • List images(01.png) and descriptions(01.txt) from directory
  • Copy to all folders batch file?
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • C: Incompatible pointer type initializing
  • Install PHP intl extension on MacOS
  • How to define and use opencv mat of user type
  • How to test if a URL from an Eclipse bundle is a directory?
  • Can Jackson SerializationFeature be overridden per field or class?
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Cassandra Data Model
  • Symfony2: How to get request parameter
  • Redux, normalised entities and lodash merge
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • AT Commands to Send SMS not working in Windows 8.1
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • PHP: When would you need the self:: keyword?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Android Studio and gradle
  • Acquiring multiple attributes from .xml file in c#
  • Getting Messege Twice Using IMvxMessenger
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • reshape alternating columns in less time and using less memory
  • Python/Django TangoWithDjango Models and Databases
  • How can I use threading to 'tick' a timer to be accessed by other threads?