The temporary is due to the AddFileToFolder further down, which you would simply be moving.  I'm not sure what use a function is that just alters the slash and encoding on a filename, when you always need to add it to the basepath anyway?