If you want to upload a file to local server, you will get an error possibly. But your code works on server. You are not wrong. Thats because of folder permissions. First of all find the php.ini file under /Applications/xampp/etc directory and change upload_tmp_dir variable to writable directory.
Second way that I have used :
find this line in php.ini file and go to temp folder. Change temp folder permissions to writable. Add your user account to share and permissions sections, then set your user account permissions to read and write.
temp folder location in php.ini folder