Darko Miletic
Darko Miletic

Reputation: 1356

PHP and file attributes across operating systems

How to determine if a file is marked as hidden using only PHP functions? This applies especially on Windows and Linux.

Upvotes: 2

Views: 1384

Answers (1)

Andrew Moore
Andrew Moore

Reputation: 95364

In a UNIX system, a file is hidden if its name starts with a dot (.).

In Windows, a file is hidden if it has the hidden attribute.

You can create a function which checks the attributes under windows and checks the file name under a POSIX compliant system as such:

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

Upvotes: 2

Related Questions