Transferred files can be truncated if you do not call ssh2_exec($connection,'exit'). ssh2_scp_send will return true even if the file did not completely transfer.
eg.
ssh2_scp_send($connection, $infile, $remotefile, 0644);
ssh2_exec($connection, 'exit'); // exit the ssh2 connection