Доброго времени суток. Хотел бы спросить. Как можно реализовать одновременную, последовательную отсылку файлов в одном запросе?
Есть скрипт(закомментированная версия для сбора файлов из нескольких форм)
<script type="text/javascript">
session_random_num=Math.floor(Math.random()*4000);
document.getElementById("my").onclick=function(){
var num=document.forms.upload.getElementsByTagName("input").length;
//var i=0;
var param={};
//while(i<num-1){param[document.forms.upload.getElementsByTagName("input")[i].getAttribute("name")]=document.forms.upload.getElementsByTagName("input")[i].files[0];i++;}
param[document.forms.upload.getElementsByTagName("input")[0].getAttribute("name")]=document.forms.upload.getElementsByTagName("input")[0].files;
console.log(param);
//$.post('http://localhost/file.php',param);
upload(document.forms.upload.getElementsByTagName("input")[0].files);
return false;
};
function upload(file, onSuccess, onError, onProgress) {
var xhr = new XMLHttpRequest();
xhr.onload = xhr.onerror = function() {
if(this.status != 200 || this.responseText != 'OK') {
return;
}
};
xhr.upload.onprogress = function(event) {
}
xhr.open("POST", "file.php", false);
xhr.onreadystatechange = function() {
// status=0 при ошибках сети, иначе status=HTTP-код ошибки
alert('Ошибка ' + xhr.status + ': ' + xhr.statusText); }
console.log(xhr.responseText);
xhr.send(file);
}
</script>
и обработчик
<?php
header("Content-type:text/html;charset=windows-1251");
$i=0;
$error_num=0;
while($i<count($_FILES['file']["tmp_name"]))
{
$move=move_uploaded_file($_FILES['file']['tmp_name'], '/'.$_POST["session_name"].$_FILES['file']['name'][$i]);
if (!$move) $error_num++;
$i++;
}
if($error_num>0) die("Незагруженно не одного файла".print_r($_FILES['file']['tmp_name']));
else echo "true";
?>
Который на выходе возвращает true, но файлы не сохранятся. Хотел бы спросить. Как можно отправить сразу несколько файлов на сервер одним запросом?