Don't forget that you can't be connected to the mailbox that you are going to rename (or delete).
If you have the following mailboxes:
INBOX
INBOX.Foo
...and want to rename "INBOX.Foo" to "INBOX.Bar" you have to be connected to "INBOX":
<?php
$mbox = imap_open('{imap.example.com}INBOX', 'username', 'password');
imap_renamemailbox($mbox, '{imap.example.com}INBOX.Foo', '{imap.example.com}INBOX.Bar');
imap_close($mbox);
?>