ftp_site()不想更改文件夹上的CHMOD

I have a joomla page on which I want to install a few extensions. However due to the chmod permissions I am unable to upload and install the packages. Following the guide here I can see that in order for the plugins to be installed there are too many folders which permissions have to be changed.

For that I am creating a script which will iterate through each of the required folders and will change the permissions from 0775 to 0777.

<?php
// SET THE DESIRED CHMOD VALUE
if ($_GET['chmod']) { 
        $ftp_chmod = $_GET['chmod']; 
    } 
    else { 
        $ftp_chmod = "0755"; 
    }
echo "chmod=" . $ftp_chmod . '<br />';
echo getcwd() . '<br />';
$currdir = getcwd(); // get current directory

// ESTABLISH AN FTP LOGIN SESSION
$ftp_server='example.com';
$ftp_user='username';
$ftp_pass='*****';
$conn_id = ftp_connect("$ftp_server");

if ( ftp_login($conn_id, $ftp_user, $ftp_pass) ) {
    echo 'FTP CONNECTION IS SUCCESSFULL <br />';
}
else {
    echo 'BAD CREDENTIALS';
    exit();
}

// Define the folders for which the CHMODE will change the values
// There must be a leading space in front of the path in order for CHMOD to work
$folder_path = array(
    ' ' . $currdir . '/modules/',
    ' ' . $currdir . '/plugins/'
    ' ' . $currdir . '/tmp/',
    ' ' . $currdir . '/cache/'
);

echo '<br />';
foreach ( $folder_path as $key => $value )
{
    $path = trim($value); // The leading space must be trimed fo is_dir() function to work

    if ( is_dir($path) == true ) {

        echo $path . ' -- ' . '<span style="color: #00B200">OK</span><br />';
        echo 'CHMOD ' . $ftp_chmod . '  ' . $value . '<br />';

        if (ftp_site($conn_id, 'CHMOD ' . $ftp_chmod . $value)) {
            echo 'CHMOD ' . $ftp_chmod . ' IS <span style="color: #00B200">SUCCESSFULL</span><br /><br />';
        }
        else {
            echo '<span style="color: crimson">CHMOD FAILED!</span><br /><br />';
        }
    }
    else {
        echo $path . ' -- ' . '<span style="color: crimson"><b>NOT EXIST</b></span><br />';
    } // end if ( is_dir($path) == true ) else

} // end foreach ( $folder_path as $key => $value )

ftp_close($conn_id);
?>

Please note that the actual script is much larger due to the many folders that need to be changed. The folders shown in $folder_path = array() are just an example

When I execute the script on my server i get the folowind output:

chmod=0777
/var/www/example/data/www/example.com
FTP CONNECTION IS SUCCESSFULL 

/var/www/example/data/www/example.com/modules/ -- OK
CHMOD 0777 /var/www/example/data/www/example.com/modules/
CHMOD FAILED!

/var/www/example/data/www/example.com/plugins/ -- OK
CHMOD 0777 /var/www/example/data/www/example.com/plugins/
CHMOD FAILED!

/var/www/example/data/www/example.com/tmp/ -- OK
CHMOD 0777 /var/www/example/data/www/example.com/tmp/
CHMOD FAILED!

/var/www/example/data/www/example.com/cache/ -- OK
CHMOD 0777 /var/www/example/data/www/example.com/cache/
CHMOD FAILED!

DOES ANYONE HAVE ANY IDEA ON HOW TO CHANGE THE CHMOD VALUE ON SO MANY FOLDERS?


UPDATE:
I also have to mention that I have tried to change the CHMOD value on each folder individually via FTP client and it is successfull. The problem comes when i have to change them through the script. The same acc with root privilege access is used from the FTP client and the script to change the files!

I have found a solution to the problem. What I did was a small modification to the foreach part of the script which made it work. Basically I just subtracted the required number of characters from the path returned from the getcwd() and that solved my problem.

Here is the change that i did:

---- REST OF THE SCRIPT OMITED -----

foreach ( $folder_path as $key => $value )
{
    $path = trim($value); // The leading space must be trimed fo is_dis() function to work

    if ( is_dir($path) == true ) {

        $value_short = substr($value, 19); // <------------- Subtruct the required number od chars in order to create a relative path

        echo $path . ' -- ' . '<span style="color: #00B200">OK</span><br />';
        echo 'CHMOD ' . $ftp_chmod . '  ' . $value_short . '<br />';


        if (ftp_site($conn_id, "CHMOD $ftp_chmod $value_short")) { // <-------------- Use the relative path in the function
            echo 'CHMOD ' . $ftp_chmod . ' IS <span style="color: #00B200">SUCCESSFULL</span><br /><br />';
        }
        else {
            echo '<span style="color: crimson">CHMOD FAILED!</span><br /><br />';
        }
    }
    else {
        echo $path . ' -- ' . '<span style="color: crimson"><b>NOT EXIST</b></span><br />';
    } // end if ( is_dir($path) == true ) else

} // end foreach ( $folder_path as $key => $value )

---- REST OF THE SCRIPT OMITED -----

Did you try just enabling Joomla's FTP layer before installing components/plugins etc?