There is some form for sending a file / image to the server and there the file should be saved in the handler, and then give the file name in the response.
The file itself is loaded onto the disk correctly, without errors. And the Response itself also comes, only then the page is immediately updated and forgets about it.
Form to send a file / image to the server:
<form name="upload" method="POST" enctype="multipart/form-data"> Отправить этот файл: <input name="newfile" type="file" /> <button id="submit"> Отправить </button> </form> <div id="log"></div> And using the built-in XMLHttpRequest object, I try to send the image to a php script for further conversion and saving.
<script> document.forms.upload.submit.onclick = function() { var formData = new FormData(document.forms.upload); var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { console.log("response = "+xhr.responseText); write(xhr.responseText); } } try { xhr.open("POST", "/upload", false); } catch(e) { console.log('Ошибка ' + e.name + ":" + e.message + "\n" + e.stack); } xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest"); xhr.send(formData); } function write(text) { var p = log.appendChild(document.createElement('p')); p.innerHTML = text; } </script> So the handler itself, which was written using the lightweight slim-framework, helped me more easily handle the receipt and sending of messages / requests. The file itself is saved, there is nothing important here.
<?php $app->post('/upload', function ($request, $response, $args){ try { $files = $request->getUploadedFiles(); if (empty($files['newfile'])) { throw new Exception('Expected a newfile'); } $newfile = $files['newfile']; if ($newfile->getError() === UPLOAD_ERR_OK) { $uploadFileName = $newfile->getClientFilename(); $newfile->moveTo($uploadFileName); } else { throw new Exception('Error '. $newfile->getError());} $result = $uploadFileName; return $result; } catch ( Exception $e ) { $file = 'log.txt'; $log = "Выброшено исключение: ".$e->getMessage().' '+$uploadFileName; file_put_contents($file, $log, FILE_APPEND | LOCK_EX); } }); 