Вот решил сделать массовую загрузку файлов с использованием XMLHttpRequest,
но есть одна проблема один файл грузит нормально а массово нет, в консоле
вижу одну и туже ошибку Uncaught SyntaxError: Unexpected token {
помогите разобраться пожалуйста..
function ge(i){
return document.getElementById(i);
}
var content = {
current_file: 0,
xupl: function(url){
var uploadfile = ge("uploadfile");
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener('progress', function(e){
if(e.lengthComputable){
var p = (e.loaded/e.total)*100;
$('.upload_progress_proc').css('width', p+'%');
$('.upload_progress_bl').show();
}
}, false);
xhr.onreadystatechange = function(e){
if(e.target.readyState == 4){
if(e.target.status == 200){
var d = JSON.parse(xhr.responseText);
if(d.status == 'status_bad_format') $('#type_error').show().text('Не соответствующий формат. Или же файл не был передан: '+d.files);
else if(d.status == 'status_big_size') $('#type_error').show().html('Слишком большой файл');
else if(d.status == 'status_not_album') $('#type_error').show().html('Такого альбома не существует');
else if(d.status == 'status_yes'){
$('#type_error').show().text('Файл загружен успешно');
console.log(d.name);
}
else if(d.status == 'status_no') $('#type_error').show().text('Не возможно загрузить файл');
console.log(d);
}
}
};
xhr.open('POST', url, true);
var data = new FormData();
for(var i = 0; i < uploadfile.files.length; ++i){
data.append('uploadfile[]', uploadfile.files[i]);
}
xhr.send(data);
},
xbpl: function(url){
$('#uploadfile').click();
$('#uploadfile').change(function () {
content.xupl(url);
});
}
}
<?php
header('Content-type: text/html; charset=utf-8');
header('Content-Type: application/json');
if(isset($_FILES['uploadfile']['tmp_name'])){
// Форматы файлов изображения с учотом большой и маленькой буквы
$allowed_files = array('jpg', 'jpeg', 'jpe', 'png', 'gif');
foreach ($_FILES['uploadfile']['name'] as $name => $value){
// Загружаем файл в временую папку на сервере
$image_tmp = $_FILES['uploadfile']['tmp_name'][$name];
// Оригинальное имя файла на сервере
$image_name = stripslashes($_FILES['uploadfile']['name'][$name]);
// Изменяем имя фотографии с учотом времени сервера md5 + rand
$image_rename = substr(md5($server_time+rand(1,100000)), 0, 15);
// Вычесляем размер файла
$image_size = $_FILES['uploadfile']['size'][$name];
// Вычесляем формат файла
$type = end(explode(".", $image_name));
// Проверяем ли присутствует в списке такой формат, если нет то выдаем ошибку
if(in_array($type, $allowed_files)){
if($image_size < 1000000){
$res_type = '.'.$type;
// Директория папки пользователя
$uploaddir = 'uploads/';
if(move_uploaded_file($image_tmp, $uploaddir.$image_rename.$res_type)) {
$json = json_encode(array('status' => 'status_yes', 'name' => $image_rename.$res_type));
echo $json;
} else {
$json = json_encode(array('status' => 'status_no'));
echo $json;
}
} else {
$json = json_encode(array('status' => 'status_big_size'));
echo $json;
}
} else {
$json = json_encode(array('status' => 'status_bad_format', 'files' => $image_name));
echo $json;
}
}
die();
}
?>
а вот сам исходник
http://rghost.ru/56801644
За ранние спасибо..