This example shows you how to build a file upload handler that lets users select a file they wish to upload along with a name for the file just in case they want to specify something other than the file name. The function then takes the data stream, moves the temporary file to a location of your choosing on the server, renames the file to match that of either the file name that was uploaded or if a name was provided on the input form it will rename it to that, adding underscores where spaces appear. Several error checks are performed along the way to ensure that everything happens as we expect it to.
The following is a screenshot of the form itself after doing a submit, you will see the $_POST data along w/ the $_FILES data. This is helpful for both debugging as well as understanding what is going on behind the scenes.
In the $_POST array you will see two members, the first is a hidden form field value which defines the max file size and the second is 'sFile_Name' which is the value from the input field on the form. The $_FILES array contains the following information about the file: type [the mime type of the file uploaded], tmp_name [the location/name of the temporary file stored on the server], error [the error code reported by the server, 0 or UPLOAD_ERR_OK means no error occurred], and finally size [the size of the file].
Now for the code, everything is fairly documented and pretty straight forward. The biggest thing to keep in mind outside of the PHP code is that the form enctype must be set to "multipart/form-data" otherwise the file will not be submitted to the server. If you have any questions please leave them for me in the comments and I will reply as soon as I see them.