Hi all

I am using the following function to chmod directories:

PHP Code:
function chmod_open($ftp_user_name,$ftp_user_password,$ftp_root,$ftp_server,$path,$dir){
    if(!
ftp_connect($ftp_server)){ // make sure $ftp_server is right
        
header('location:test_permissions.php?test2&ftp_connect_fail');
    }else{
        
$conn_id ftp_connect($ftp_server);
        if(!
ftp_login($conn_id$ftp_user_name$ftp_user_password)){ // make sure $ftp_user_name and $ftp_user_password are right
            
header('location:test_permissions.php?test2&ftp_login_fail');
        }else{
            
$chmod_message '<p>Success/Error Messages:</p>';
            foreach(
$dir as $d){
                if(
is_dir($d)){
                    if(
ftp_site($conn_id'CHMOD 0777 '.str_replace('//','/',$ftp_root.$path.$d))){ 
                        
$chmod_message .= '<p style="color:green;">Chmod success for: '.$path.$d.'</p>';
                    }else{
                        
$chmod_message .= '<p style="color:red;">Chmod failure for: '.$path.$d.'</p>';
                    }
                }elseif(
is_file($d)){
                    if(
ftp_site($conn_id'CHMOD 0666 '.str_replace('//','/',$ftp_root.$path.$d))){ 
                        
$chmod_message .= '<p style="color:green;">Chmod success for: '.$path.$d.'</p>';
                    }else{
                        
$chmod_message .= '<p style="color:red;">Chmod failure for: '.$path.$d.'</p>';
                    }
                }
            }
            
ftp_close($conn_id);
            echo 
'...'.$chmod_message;

            }
        }
    }

Then I call the function:
PHP Code:
$dir = array('site_content/background_images','site_content/home title and description','site_content/section 1','site_content/section 2','site_content/xxx');
chmod_open($ftp_user_name,$ftp_user_password,$ftp_root,$ftp_server,$path,$dir); 
Of course, $ftp vars ($ftp_user_name and so on) are correctly defined. I have tested it on two servers. On one, it works perfectly.
On the other one, it hangs... and hangs... for minutes! once in a while, it works half way, sometimes it works all the way, but 90&#37; of the time it just hangs for ever until it times out. In the above code, I chmod an array of directories, but even if I try to chmod only one directory, it hangs the same way, and only works one out of 10 times...
How can I figure out why it's hanging like that on this server?