Issue
It seems
chmod("*.txt", 0660);
doesn't work.
I know I can:
- chmod the files one by one: it works but I do not know all the names in advance, so I cannot use it.
- Use
exec
. It works but I don't like it for several reasons (speed, security, and so on). - Use
scandir
. It works but again, slow and I guess too much for a simple operation
I really want to use chmod directly. Is it even possible? Thank you.
Solution
So you can do this using scandir
like you mentioned and yes filesystem can be pretty slow, you can add a check in so you do not do it to files you have already processed
<?php
$files = scandir('./');
foreach ($files as $file) {
// check here so you don't have to do every file again
if (substr(sprintf('%o', fileperms($file)), -4) === "0660") {
echo "skipping " . $file;
continue;
}
$extension = pathinfo($file)['extension'];
if ($extension === 'txt') {
chmod($file, 0660);
}
}
Or you could use glob
<?php
$files = glob('./*.{txt}', GLOB_BRACE);
foreach($files as $file) {
// check here so you don't have to do every file again
if (substr(sprintf('%o', fileperms($file)), -4) === "0660") {
echo "skipping " . $file;
continue;
}
$extension = pathinfo($file)['extension'];
if ($extension === 'txt') {
chmod($file, 0660);
}
}
Answered By - Chris Townsend Answer Checked By - Terry (WPSolving Volunteer)