tags:

views:

15

answers:

1

Can comebody tell me how to determine if a file is marked as hidden using only PHP functions? This applies especially on Windows and Linux.

+1  A: 

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] === '.');
    }
}
Andrew Moore
It sort of responds to my question. But you are using external command line app to distinguish hidden from non-hidden files. That also implies that there is no PHP native way of doing this on windows. Thanks for your response.
Darko Miletic
@Darko Miletic: There is no Pure-PHP way (at least not without a non-default PHP extension) of finding out if the file is hidden or not in Windows. You can count on `attrib` to be available under every installation of Windows and the solution above will work consistently.
Andrew Moore