This solution to a common problem is implied elsewhere, but I thought it might be useful to put it all in one place (since I spent hours piecing it together!)
Sometimes a web host will open PHP sessions with a user of 'nobody'. Files created by this user may not have the correct permissions to allow management of those files by the actual owner of the site. The following script allows the actual owner to open access to a directory so that 'nobody' can create a file using fopen(). Then using the handle created by 'nobody', the ftp_fput() command saves the file with the correct owner. The file 'nobody' created is discarded.
<?
$connection = ftp_connect($ftpServer);
ftp_login($connection, $ftpUser, $ftpPass);
ftp_chdir($connection, $ftpDir);
// open the directory so that 'nobody' can create a temporary file
ftp_site($cn, "CHMOD 777 $ftpDir");
$new="tempFile";
@unlink($new); // just in case
$handle=fopen($new,"x");
chmod($new,0777);
fputs($handle,"a bunch of stuff...");
fclose($handle); // have to rewind
$handle=fopen("tempFile","r");
ftp_fput($connection, "finalFile", $handle, FTP_ASCII);
fclose($handle);
unlink($new);
// remove open access to the directory
ftp_site($connection, "CHMOD 755 $ftpDir")
ftp_close($connection);
?>