The pathinfo function works only for “valid” filenames. You could remove or replace all invalid characters before you use the pathinfo function. Generally, it is never very “safe” to use the client’s filename. So maybe consider a “fallback” to detect the correct extension.
You need only one request for the upload. The response from the POST (image upload) request could return a JSON object with the generated object values.