You can't fix the script to prevent that. If you are planning to upload files that contain spaces, the best way to upload them is to use the upload progress bar. It will auto correct for the spaces.