Although there are some reports on the Internet that PHP chmod can actually set Windows attribute flags (at least read-only), I could not reproduce this at all.
So bypassing the attrib is the way to go.
READ-ONLY for Windows and * nix
Here is some code to install a read-only file that will work on Windows and * nix:
// set file READ-ONLY (Windows & *nix) $file = 'path/to/file.ext'; if(isset($_SERVER['WINDIR'])) { // Host OS is Windows $file = str_replace('/', '\\', $file); unset($res); exec('attrib +R ' . escapeshellarg($file), $res); $res = $res[0]; }else{ // Host OS is *nix $res = chmod($file, 0444); } //$res contains result string of operation
Tips:
Replacing '/' with '\' is important because the shell command ( attrib ) is not slash-tolerant like PHP does.
$ res is not installed on the Windows part because exec () adds any existing value.
HIDDEN on Windows
If you want to hide the file, this will probably be a task only for Windows:
// set file HIDDEN (Windows only) $file = 'path/to/file.ext'; $file = str_replace('/', '\\', $file); unset($res); exec('attrib +H ' . escapeshellarg($file), $res); $res = $res[0]; //$res contains result string of operation
source share