There is a form. Most of the data from it is collected and processed in PHP, except for one input.
<div id="drop-zone"> <p>Перетащите файлы сюда...</p> <div id="clickHere">или нажмите здесь.. <i class="fa fa-upload"></i> <input type="file" name="file[]" id="files" multiple /> </div> <div id='filename'></div> </div> I collect all this into an array (if necessary - I can show the code), I send the array as follows:
var xhr = new XMLHttpRequest(); xhr.open('POST', 'http://zayavlenie.com.ua/mail-dev.php', true); // The rest of the code will go here... // Set up a handler for when the request finishes. xhr.onload = function() { if (xhr.status === 200) { // File(s) uploaded. uploadButton.innerHTML = 'Отправка'; } else { alert('An error occurred!'); } }; console.log(formData); console.log(finalFiles); xhr.send(finalFiles); if (xhr.status != 200) { // обработать ошибку alert(xhr.status + ': ' + xhr.statusText); // пример вывода: 404: Not Found } else { alert(xhr.responseText); // responseText -- текст ответа. } } On the server side, I try to listen to everything that comes in this way:
<? header('Access-Control-Allow-Origin: *'); $post = $_POST; var_dump($post); echo('<hr>'); $req = $_REQUEST; var_dump($req); echo('<hr>'); At the output, I receive data from fomry that are processed by the php script, but not the value of the field input type="file" The $ _FILES array returns the following
array(1) { ["file"]=> array(5) { ["name"]=> array(1) { [0]=> string(0) "" } ["type"]=> array(1) { [0]=> string(0) "" } ["tmp_name"]=> array(1) { [0]=> string(0) "" } ["error"]=> array(1) { [0]=> int(4) } ["size"]=> array(1) { [0]=> int(0) } } } string(0) "" Warning: fopen(): Filename cannot be empty in /var/www/zayavlenie/zayavlenie.com.ua/mail-dev.php on line 227 Warning: fread() expects parameter 1 to be resource, boolean given in /var/www/zayavlenie/zayavlenie.com.ua/mail-dev.php on line 228 Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/zayavlenie/zayavlenie.com.ua/mail-dev.php on line 229 How to send data correctly using xhr.send ()