PHP windows create hidden files
Asked Answered
T

3

6

Is it possible to create hidden files/folders on windows using php (xampp)? And if it is, how?

Taco answered 8/2, 2011 at 16:32 Comment(1)
In wWindows you can make files hidden so that they won't be visible by default to the users, only to the OS.Taco
C
12

A file in Windows is hidden if it has the hidden attribute set on it. There is no built in function to do this, so you need to use system/exec to execute the attrib application. Like this:

$file = 'test.txt';
system('attrib +H ' . escapeshellarg($file));

This will set the hidden (+H) flag on test.txt.

Caritacaritas answered 8/2, 2011 at 16:39 Comment(0)
H
2

You could call attrib:

$filename = 'c:\\some\\file.txt';
exec('attrib +h '.$filename);
Homestretch answered 8/2, 2011 at 16:37 Comment(0)
K
0
// set HIDDEN attribute of file on Windows
$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

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

If you are looking for a way to set a file to read-only that will work on Windows AND *nix, then have a look at my answer to this other question: https://mcmap.net/q/1770119/-is-there-a-way-to-toggle-the-quot-hidden-quot-or-quot-read-only-quot-switches-on-a-windows-file-using-php

Klenk answered 25/11, 2014 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.