Mac / MAMP 上 PHP 文件路径大小写不一致?

发布于 2024-12-09 08:52:01 字数 925 浏览 0 评论 0原文

我正在 MAMP 上开发 PHP 程序,刚刚意识到以下奇怪的行为:

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

include.php:

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

结果:

PATH = /users/me/stuff/mamp_server/my_site(全部小写)

PATH = /Users/me/Stuff/mamp_server/my_site(混合大小写)

是什么导致了这种不一致的行为,以及如何防止这种情况发生? (请注意,我不能将所有内容都转换为小写,因为该应用程序是针对 Linux 服务器的,其中文件路径区分大小写。)

更新:

__FILE__和<代码>__DIR__。

看起来这可能是一个真正的问题,没有解决办法......除非我听到其他消息,否则我将提交错误报告。

错误报告:

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

更新:

另请注意:如果您在 Mac 上执行绝对路径 include(...),则需要混合大小写版本。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

执着的年纪 2024-12-16 08:52:01

我在 MAC OS X 上开发 PHP 时遇到了类似的问题。您可以使用区分大小写的文件系统进行格式化,但如果您使用 Adob​​e 的设计软件,您可能会遇到麻烦:http://forums.adobe.com/thread/392791

真正的问题是,据说不区分大小写的文件系统实际上是部分不区分大小写的。您可以在同一目录中创建两个名为“Filename”和“filename”的文件,但“Filename”和“filename”可能指向这两个文件: http://systemsboy.com/2005/12/mac-osx-command-line-is-partially-case-insensitive.html

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

猫七 2024-12-16 08:52:01

在与您的应用程序相同的目录中创建一个包含文件怎么样?

<?php return __DIR__; ?>

像这样使用它:

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

从您上面发布的内容来看,这应该可行。是的,这是一个有点棘手的解决方法,但它是一种解决方法,即使在没有遇到此问题的系统上也应该有效。

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.

清风夜微凉 2024-12-16 08:52:01

这是我用来获取给定文件名的正确大小写的代码:

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]).']';
}

glob 应该只匹配一个文件,但如果在区分大小写的文件系统上使用,那么它可以匹配更多文件 - 所需的行为取决于您 - 例如返回 < code>[0] 无论如何,或者抛出一个 E_NOTICE 或其他东西。

您可能会发现它很有帮助: $mydir = get_cased_filename(dirname(__FILE__)); 适用于 Mac 10.6.8 上的 CLI PHP 5.3.6。

我用它来处理那些没有注意到“文件名”和“文件名”不同的同事。 (这些人还想知道为什么包含“>”或“?”的文件名在从 Mac 复制到 Windows 服务器时不起作用,但我离题了......)

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

梦情居士 2024-12-16 08:52:01

我正在使用 apache,我发现执行文件的 __DIR__ 与 apache 配置的 DOCUMENT_ROOT 中找到的文件相同。

这意味着如果 apache 配置有

DocumentRoot /users/me/stuff/my_site

问题中的脚本正在打印:

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

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

如果 apache 配置有:

DocumentRoot /Users/me/stuff/my_site

问题中的脚本正在打印:

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

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

哪个更好。

如果遇到此问题,请检查 apache 配置,并考虑到它区分大小写。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文