代码之家  ›  专栏  ›  技术社区  ›  Darko Miletic

跨操作系统的PHP和文件属性

  •  2
  • Darko Miletic  · 技术社区  · 14 年前

    如何确定文件是否仅使用PHP函数标记为隐藏?这尤其适用于Windows和Linux。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Andrew Moore    14 年前

    . ).

    在Windows中,如果文件具有hidden属性,则该文件是隐藏的。

    function file_hidden($file) {
        if (!file_exists($file))
            return false;
    
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $attributes = shell_exec('attrib ' . escapeshellarg($file));
    
            // Just get the attributes
            $attributes = substr($attributes, 0, 12);
    
            if ($attributes === 'File not fou')
                return false;
    
            // Return if hidden
            return (strpos($attributes, 'H') !== false);
        } else {
            $basename = basename($file);
    
            return ($basename[0] === '.');
        }
    }