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// Assume that this is the value returned from the database.$mode = 644;

// Now try to chmod using this value.ftp_chmod($conn_id, $mode, 'test.txt');

// The file now has permissions of 204 and not 644?>

Adding a leading zero doesn't work either:

<?php// Assume that this is the value returned from the database.$mode = 644;

// Now try to chmod using this value.ftp_chmod($conn_id, '0'.$mode, 'test.txt');

// The file now has permissions of 204 and not 644?>

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// Assume that this is the value returned from the database.$mode = 644;

// Turn the mode into a string$np = '0'.$mode;

// Now run chmod with the eval'd string parsed as an integer.ftp_chmod($conn_id, eval("return({$np});"), 'test.txt');

// The file now has permissions of 644?>

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().