Home  >  Article  >  Backend Development  >  php根据操作系统转换文件名大小写的方法_php实例

php根据操作系统转换文件名大小写的方法_php实例

WBOY
WBOYOriginal
2016-05-17 08:49:021258browse

在php的开发中我们要包含某个文件通常的代码是这样的:

复制代码 代码如下:

     if(is_file($fileName))
        require $flleName;

在windows,linux下运行都没有问问题: 假设现在要包含一个   D:/web/webServer/A.php文件

在传值的时误传了D:/web/webServer/a.php  在windows下运行时也会把D:/web/webServer/A.php包含进来,因为windows是不区分大小的,放在linux下就会报错了

那如做到在windows加载也能区分大小写呢?代码如下:

复制代码 代码如下:

if(is_file($fileName)){
   //PHP_OS  当前运行的操作系统
   if(strstr(PHP_OS,'WIN')){
     //realpath($fileName) 会转换文件名的大小写  /web/A.php 如果A.php不存在而a.php则会返回/web/a.php
        if(basename(realpath($fileName)) == basename($fileName))
            require $fileName;
        else
            echo '请检查文件的大小写';
    }else
        require $fileName;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn