Sending a png file via AJAX for PHP. var_dump in PHP shows the following:
string (1462575) "------ WebKitFormBoundaryh7lE1zEduW95eI64 Content-Disposition: form-data; name =" file "; filename =" _ keys_inunity.png "Content-Type: image / png PNG IHDR D ....
When I try to view a saved image, I get an error that the file is damaged.
How to save via PHP file received via AJAX?
<form> <input type="file" id="file" name="file"> <input type="submit"> </form> <script type="text/javascript"> $('form').submit(function (e) { var data; data = new FormData(); data.append('file', $('#file')[0].files[0]); $.ajax({ url: 'http://abc-site.com/script.php', data: data, processData: false, type: 'POST', contentType: 'multipart/form-data', beforeSend: function (x) { if (x && x.overrideMimeType) { x.overrideMimeType("multipart/form-data"); } }, mimeType: 'multipart/form-data', success: function (data) { alert(data); } }); e.preventDefault(); }); </script> php $content = file_get_contents("php://input"); $name = explode('filename="', $content)[1]; $name = explode('"', $name)[0]; var_dump($content); file_put_contents($name, $content);