Page 1 of 1

Setting file permissions with PHP

Posted: Tue Nov 02, 2004 8:18 am
by mikeb
Hi,

long time ago I posted a question about creating a CMS which would write standard .htm files to the server so negating the need for long ?url variables which Google doesn't like! Anyhoo, having created a system which does all of this, I was wondering is there a way to set file permissions on multiple files on the server. I know that I can use CHMOD with php, but the script examples on php.net just show setting an individual file. Is there a way of using wildcards e.g. *.htm etc?

cheers,

Mike

Posted: Tue Nov 02, 2004 11:25 am
by rehfeld
this scans the directory of your choice, and matches all files that end in htm(this wont match html)

then it chmod's all the matches

if you need it to match more than 1 type of file ext let us know, its easy

Code: Select all

<?php

$dir_name = 'cache';

$dir = dir($dir_name);


$documents = array();
while (false !== ($document = $dir->read())) {

    if ('htm' === end(explode('.', $document))) {
        $documents[] = $document;
    }
}

$dir->close();

foreach ($documents as $doc) {
    chmod("$dir_name/$doc", 0777);
}

?>

Posted: Thu Nov 11, 2004 10:23 am
by mikeb
Nice one, thanks for that :D

Is it possible to chmod a directory. I've used chmod in php in the form of

Code: Select all

chmod("$filepath/mydirectory/filename.php", 775);
This works well and the mode of 'filename.php' is changed. But if I simply change my script to:

Code: Select all

chmod("$filepath/mydirectory/", 775);
The permissions for the 'mydirectory' directory don't get changed. Is there a different syntax when working with directories as distinct from files? (incidently, I've tried it with and without the trailing slash)

cheers,

Mike

Posted: Thu Nov 11, 2004 10:28 am
by neophyte
rehfeld wrote:

Code: Select all

<?php


$dir->close();



?>
What directory class are you using?

Posted: Thu Nov 11, 2004 10:45 am
by mikeb
Erm... sorry for being thick but directory class?