The Hawk
The Hawk

Reputation: 1568

PHP and Symlinks in Windows

I have a directory tree with symlinks in it. They are called with require_once, but sometimes they are referred to with 'm' and sometimes 'mydir'. 'm' and 'mydir' are symlinked, but when the require_once is called twice, it treats them as different files and the code errors.

require_once("m/myfile.php");
require_once("mydir/myfile.php);

I only want the file included once but it tries to do it twice.

Upvotes: 1

Views: 3836

Answers (3)

Thomas Lauria
Thomas Lauria

Reputation: 908

Some years later under windows 10 I had similar problems:

  • I checked out our project via git, symlinks activated in git
  • In the project some directory symlinks are used
  • Apache and bash under windows were working without any problems with the generated symlinks
  • PHP, Windows Explorer and CMD could not deal with the symlinks

The problem was, that the symlinks were created as "file" symlinks, but were pointing to directories. After removing the symlinks and recreating them manually with

mklink /D link target

everything was working as expected.

Upvotes: 4

Gavin
Gavin

Reputation: 2183

I would use my own require function / autoloading if fully OO http://php.net/manual/en/language.oop5.autoload.php, or a fix path function.

i.e.

//custom require
function myRequireOnce($filePath)
{//ensure only one version of sym path is ever used
  $filepath = str_replace('mydir', 'm' , $filepath); //possibly too simple
  require_once($filepath);
}

//fix path function
_fp($path)
{
  return str_replace('mydir', 'm' , $filepath);
}

require_once(_fp("m/myfile.php"));

In all cases the object is to ensure that only one path is ever actually required, and conversion takes place on the alternative versions.

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 157947

No symlinks in Windows, sorry.

If you have checked out or downloaded the code from let's say github and then dropped it to a NTFS file system, your download program may be creating the file twice once for the link and once for the linked file. I've seen this often

Upvotes: 0

Related Questions