代码之家  ›  专栏  ›  技术社区  ›  Pete

搜索php的文件名

  •  1
  • Pete  · 技术社区  · 17 年前

    我不想搜索文件名,例如这样的字符串:

    $filetype = "file.php.jpg";
    

    现在,如果文件名为php-logo.jpg,我不希望它被拒绝。也许我们应该考虑搜索.php。在字符串中?

    4 回复  |  直到 14 年前
        1
  •  1
  •   bisko    17 年前

    你为什么把事情弄得这么复杂? :)

        $file = 'some.php.file';
        if (false !== strpos($file,'.php')) {
           // file contains .php
           return false;
        }
        else {
          // file not php
        }
    
        2
  •  1
  •   alex    17 年前

    您可能需要重新考虑返回值。如果它有PHP,为什么不返回true呢?这个怎么样?

    function containsPhp($string) {
    
        return strstr($string, 'php');
    
    }
    

    那你就可以这么做了

    $filetype = "file.php.jpg";
    
    // does it contain php in it?
    
    var_dump(containsPhp($filetype));
    

    如果需要文件扩展名,请考虑以下函数。

    function getFileExtension($filePath) {
    
        $filename = basedir($filePath); // this may not be required.
    
        return pathinfo($filename, PATHINFO_EXTENSION);
    }
    

    }

        3
  •  0
  •   Aif    17 年前

    如果发送php-logo.png怎么办?

        4
  •  0
  •   erenon    17 年前
    $filetype = "file.php.jpg";
    if ( substr_count($filetype, 'php') > 0 ){
    
    } else {
        return false;
    }
    

    substr_count

    如果你只想检查扩展名:

    function get_file_extension($file_name)
    {
        return substr(strrchr($file_name,'.'),1);
    }
    if ('php' == get_file_extension($filetype)) {
    
    } else {
        return false;
    }