Skip to main content

Case-inconsistency of PHP file paths on Mac / MAMP?



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.


Comments

  1. 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

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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...)

    ReplyDelete
  4. To check for the existance of a file with the same name, but disregarding case (Credit to User Comment on PHP.net > file_exists fn):

    <?php

    /**
    * Alternative to file_exists() that will also return true if a file exists
    * with the same name in a different case.
    * eg. say there exists a file /path/product.class.php
    * file_exists('/path/Product.class.php')
    * => false
    * similar_file_exists('/path/Product.class.php')
    * => true
    */
    function similar_file_exists($filename) {
    if (file_exists($filename)) {
    return true;
    }
    $dir = dirname($filename);
    $files = glob($dir . '/*');
    $lcaseFilename = strtolower($filename);
    foreach($files as $file) {
    if (strtolower($file) == $lcaseFilename) {
    return true;
    }
    }
    return false;
    }

    ?>

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.