23

I'm developing a PHP program on MAMP, and just realized the following screwy behavior:

echo "<br/>PATH = ".dirname(__FILE__);
include 'include.php';

include.php:

<?php
echo "<br/>PATH = ".dirname(__FILE__);
?>

Result:

PATH = /users/me/stuff/mamp_server/my_site (All lower case)

PATH = /Users/me/Stuff/mamp_server/my_site (Mixed case)

What is causing this inconsistent behavior, and how can I protect against it? (Note that I can't just convert everything to lowercase, because the application is destined for a Linux server, where file paths are case sensitive. )

Update:

This problem exists for __FILE__ and __DIR__.

It looks like this might be a real problem with no work around... going to file a bug report unless I hear otherwise.

Bug report:

https://bugs.php.net/bug.php?id=60017

Update:

And another note: If you're doing an absolute path include(...) on Mac, it requires the mixed case version.

Yarin
  • 173,523
  • 149
  • 402
  • 512
  • The only idea that comes to mind is running a `realpath()` on each path and to see whether that changes anything... Otherwise, I think this is really stuff for a bug report – Pekka Oct 08 '11 at 21:47
  • Pekka- Yeah, already tried the realpath() stuff- no go – Yarin Oct 08 '11 at 21:53
  • I cannot reproduce this error. What version of PHP and Apache are you using? – afuzzyllama Oct 08 '11 at 23:35
  • @afuzz- Apache/2.2.17 PHP/5.3.6 (MAMP 2.0) – Yarin Oct 08 '11 at 23:46
  • You can configure HFS+ filesystems to be case-sensitive, perhaps that would give you the correct filenames? – sarnold Oct 08 '11 at 23:48
  • What happens if you change the case of my_site to My_site? – afuzzyllama Oct 08 '11 at 23:55
  • @sarnold- True, but that means reformatting a disk and, anyway, this is about my php code running across multiple platforms, not just my one machine. – Yarin Oct 09 '11 at 00:14
  • @afuzzyllama- it doesn't matter where the capitilization occurs- the issue is whether the path is mixed-case or not. – Yarin Oct 09 '11 at 00:19
  • I use the same setup and can't reproduce it. It always come up with the mixed case version of my tree. Same for __DIR__. Im using Snow Leopard but I guess this dosen't matter. – Talisin Oct 09 '11 at 01:09
  • I also tried it with the /Users folder as DocumentRoot but even with this I can't reproduce it. wired. – Talisin Oct 09 '11 at 01:15
  • @Talisin- that's odd- what version PHP? – Yarin Oct 09 '11 at 01:22
  • Has anyone else been able to reproduce this? – Yarin Oct 09 '11 at 01:23
  • @Talisin- Also, what happens when you do $_SERVER[DOCUMENT_ROOT]- is it lower or mixed? – Yarin Oct 09 '11 at 01:28
  • I can confirm that I can not reproduce this using MAMP on OS X Lion - Is there anything else in the original page that could be causing this? Is it just those two lines (I Assumed it was just within ) – Matt Fellows Oct 12 '11 at 07:50
  • @Matt- I've tested it with just those lines, nothing more. I've been able to reproduce it on both my MAMP-installed MacBooks- very strange that it's not reproduceable for others... – Yarin Oct 16 '11 at 14:20
  • I can confirm the problem still exists. I'm using MAMP on Yosemite. 3 years later and no solution for that :(. – Anh Tran Dec 31 '14 at 10:10
  • Just updated to 10.13 (and new filesystem APFS) and MAMP set to php 7.0.12. Even stranger issue (never experienced prior this osx update) – I've got a part of the path correctly capitalised and other part changed to lower case: actual path: `/Applications/MAMP/htdocs/.../Subtheme`, output from `__FILE__` (or `__DIR__`, or `realpath()`): `/Applications/MAMP/htdocs/.../subtheme` – Maciek Rek Nov 26 '17 at 11:21

4 Answers4

2

I have had similar problems developing PHP on MAC OS X. You can format with a case sensitive filesystem but if you are using Adobe's design software you might run into trouble: http://forums.adobe.com/thread/392791

The real issue is that the file system that is said to be case insensitive is in actual fact partially case insensitive. You can create two files with names 'Filename' and 'filename' in the same directory but 'Filename' and 'filename' may point to both files: http://systemsboy.com/2005/12/mac-osx-command-line-is-partially-case-insensitive.html

noesis
  • 21
  • 3
1

I was using apache and I found that the __DIR__ of the executed file was the same one found in the DOCUMENT_ROOT of the apache config.

That means that if the apache config had

DocumentRoot /users/me/stuff/my_site

script from the question was printing:

PATH = /users/me/stuff/my_site (All lower case)

PATH = /Users/me/stuff/my_site (Mixed case)

And if the apache config had:

DocumentRoot /Users/me/stuff/my_site

script from the question was printing:

PATH = /Users/me/stuff/my_site (Mixed case)

PATH = /Users/me/stuff/my_site (Mixed case)

Which was much better.

If you encounter this problem, check the apache configuration taking into account that it's case sensitive.

J-Rou
  • 2,216
  • 8
  • 32
  • 39
1

What about creating an include file in the same directory as your app.

<?php return __DIR__; ?>

Use it like so:

$trueDIR = include('get_true_dir.php');

From what you posted above, this should work. Yes, it's a bit of a hacky workaround, but it is a workaround, and should work even on systems not suffering from this issue.

Unsigned
  • 9,640
  • 4
  • 43
  • 72
1

This is the code I use to get the correct casing of a given filename:

function get_cased_filename($filename)
{
    $globbable = addcslashes($filename, '?*[]\\');
    $globbable = preg_replace_callback('/[a-zA-Z]/', 'get_bracket_upper_lower', $globbable);
    $files = glob($globbable);
    if (count($files)==1)
    {
        return $files[0];
    }
    return false;
}

function get_bracket_upper_lower($m)
{
    return '['.strtolower($m[0]).strtoupper($m[0]).']';
}

The glob should only match one file, but if used on a case sensitive file system then it could match more - required behaviour is up to you - eg return the [0] anyway or throw a E_NOTICE or something.

You might find it helpful: $mydir = get_cased_filename(dirname(__FILE__)); Works for my CLI PHP 5.3.6 on Mac 10.6.8.

I use it to deal with coworkers who don't notice things like "Filename" and "filename" being different. (These people also wonder why filenames that contain ">" or "?" don't work when copying from Mac to Windows server, but I digress...)

hood
  • 141
  • 5