It took me a while to figure out how to use this function in my situation because I needed the $mode to be passed to this function as a variable that was read from a database. Since the database returns the value as an integer without a leading zero, I could not get the operation to work because adding a leading zero in PHP turns the value into a string.
For example, this does not work in my situation:
<?php
$mode = 644;
ftp_chmod($conn_id, $mode, 'test.txt');
?>
Adding a leading zero doesn't work either:
<?php
$mode = 644;
ftp_chmod($conn_id, '0'.$mode, 'test.txt');
?>
I tried many ways to get it to work even converting it from oct to dec using octdec and then back to decoct and nothing worked. This is the only way I was able to get it to work, with an eval statement.
<?php
$mode = 644;
$np = '0'.$mode;
ftp_chmod($conn_id, eval("return({$np});"), 'test.txt');
?>
Of course, you will have to make sure that the value of $mode only contains 3 digits. Always do checking on your values before handing it off to eval().