Setting file permissions with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Setting file permissions with PHP

Post 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
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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);
}

?>
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Post 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
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

rehfeld wrote:

Code: Select all

<?php


$dir->close();



?>
What directory class are you using?
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Post by mikeb »

Erm... sorry for being thick but directory class?
Post Reply