Is there a way to toggle the "Hidden" or "Read-Only" switches on a Windows file using PHP?
Asked Answered
H

5

4

UPDATED

As the title says, is there a way to toggle the "Hidden" or "Read-Only" switch on Windows using PHP?

I'd like to do this without opening a shell exec() if possible.

Hepzi answered 1/12, 2010 at 7:45 Comment(3)
"Hidden" in which sense and on which operating system?Obstetrics
Yes... Under *nix systems putting leading . in file name makes it 'hidden'Sometime
@Obstetrics - Hidden, on Windows, in the sense where you have to toggle it in the "Folder Options | View" menu. @playcat - thanks, Angus mentioned that too.Hepzi
O
4

A file can't be hidden, it's always in the file system. There's the *NIX convention that files starting with a . are not shown by default for certain operations (like the ls command), but only if you don't look hard enough. The same goes for Windows, but Windows handles it with file meta attributes.

What you can/should do is use file permissions to make the folder/file inaccessible to anybody who has no business accessing it. Use chmod, chown and chgrp to do so from PHP. You may have to learn a bit about proper file system permissions though.

Obstetrics answered 1/12, 2010 at 7:52 Comment(4)
What does the "file meta attributes" part mean with respect to PHP? Is there an answer hidden in there?Hepzi
@Steve No. :) It just means that UNIX determines whether to show a file or not by looking whether or not its file name starts with a .. Windows saves meta attributes on a file and looks at those to make the same decision. It doesn't change the fact that that doesn't hide the file (in the sense that you're probably thinking), it just doesn't include it in certain operations (like Joe User looking at a folder in Windows Explorer).Obstetrics
@Steve Honestly I have no idea how to set these permissions/visibility flags on Windows. You should update your question to make clear that that's what you're looking for.Obstetrics
Those are exactly the operations I'm targeting. I don't know a better way to phrase the question without using Microsoft's terminology.Hepzi
C
3

To make a file "hidden" on Windows you can use

attrib +h yourfile.ext

To make a file "read-only" on Windows you can use

attrib +r yourfile.ext

To use these commands from PHP you just execute them using system or exec.

Also see : Attrib

Colis answered 1/12, 2010 at 8:33 Comment(2)
Is there any way to do this without opening a shell?Hepzi
With standard PHP? Not that I know of. There could be a PHP module providing this functionality though (or you could write one yourself).Colis
A
3

While there are some reports on the web that PHP's chmod would indeed be able to set Windows attribute flags (at least the read-only flag) I could not reproduce this at all.
So shelling out to an attrib command is the way to go.

READ-ONLY on Windows and *nix

Here is some code to set a file to read-only 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

Hints:
Replacing '/' with '\' is important as the shell command (attrib) is not as tolerant to slashes as PHP is.
$res is unset in the Windows part because exec() appends to any existing value.

HIDDEN on Windows

If you want to set a file hidden, this would probably be a Windows only task:

// 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
Aarau answered 25/11, 2014 at 13:8 Comment(0)
C
1

On Linux/Unix you can make the file hidden by putting a dot (.) at the start of its name, and use the chmod function to make the file read-only. Not sure about Windows.

Cecillececily answered 1/12, 2010 at 7:49 Comment(1)
Thanks Angus will have a look.Hepzi
S
0

For the file permissions, try chmod function:

<?php 
    chmod("/somedir/somefile", 0755);  // octal; correct value of mode
?>

More here: http://php.net/manual/en/function.chmod.php

Sometime answered 1/12, 2010 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.