Hello All!
How can I automatically change permissions just for files in current directory and not for other directories. I suppose that 'chmod -R 664 .'
will change permission to all current directory content..

Recommended Answers

All 5 Replies

get the contents of the folder and use "is_dir()" in an "if" statement so that you would chmod files only
:)

get the contents of the folder and use "is_dir()" in an "if" statement so that you would chmod files only
:)

Have you some recursively snippet, please?

try something like this

$files =  scandir($dir_path);
foreach ($files as $file)
{
if(!is_dir($file))
{
chmod($file, 0664)
}
}

try something like this

$files =  scandir($dir_path);
foreach ($files as $file)
{
if(!is_dir($file))
{
chmod($file, 0664)
}
}

Thank you for answering.

Following recursive snippet works for me.

<?php

print_r(ReadFolderDirectory("/var/www" ));

function ReadFolderDirectory($dir = "" ) {

    $listDir = array();

    if($handler = opendir($dir)) {

        while (($sub = readdir($handler)) !== FALSE) {
            if ($sub != "." && $sub != ".." ) {

                if(is_file($dir."/".$sub)) {


                    chmod($dir."/".$sub, 0664);
                    
                    $listDir[] = $sub;

                }elseif(is_dir($dir."/".$sub)) {

                    $listDir[$sub] = ReadFolderDirectory($dir."/".$sub);

                }
            }

        }
        closedir($handler);
    }
    return $listDir;
}

?>

you're welcome :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.