更新
正如标题所述,是否有一种方法可以使用PHP切换Windows上的“隐藏”或“只读”开关?
如果可能的话,我想在不打开shell exec()
的情况下这样做。
发布于 2010-11-30 23:52:11
发布于 2010-12-01 00:33:55
若要在Windows上“隐藏”文件,可以使用
attrib +h yourfile.ext
若要使Windows上的文件“只读”,可以使用
attrib +r yourfile.ext
要使用PHP中的这些命令,只需使用system或exec执行它们。
另见:阿特里卜
发布于 2014-11-25 05:08:57
虽然网络上有一些报道说PHP的chmod确实能够设置Windows属性标志(至少是只读标志),但我根本无法复制它。
因此,使用attrib
命令进行轰击是可行的方法。
只读在Windows和*nix上
下面是一些将文件设置为只读的代码,这些代码将在Windows和*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
提示:
用'\‘替换'/’很重要,因为shell命令(attrib
)对斜杠的容忍度不如PHP。
$res在Windows中未设置,因为exec()附加到任何现有值。
隐藏在Windows上的
如果要设置隐藏的文件,这可能是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
https://stackoverflow.com/questions/4322215
复制相似问题