我正在尝试将“大”文件上传到我的应用程序。用户必须能够上传小于200MB的视频文件,但似乎服务器在4MB或30秒后丢弃连接并且上传失败。
我已经在php.ini
文件中设置了所有参数:
max_input_time = 320
max_execution_time = 320
max_file_uploads = 20
memory_limit = 512M
post_max_size = 201M
upload_max_filesize = 200M
当我上传2MB @ 1Mbps / s的文件时,一切正常(我不知道文件大小或传输时间是否有问题)
可以通过php_info
访问实时php_info()文件虽然这是DropZone.js conf:
$("#dZUpload").dropzone({
url: "/ajax/admin/admin.acceptVideo.php",
maxFilesize: 209715200,
acceptedFiles: "video/*",
addRemoveLinks: true,
dataType: "HTML",
data: { id: '' },
success: function (file, response, data) {
var imgName = response;
file.previewElement.classList.add("dz-success");
$('#form_video').val(imgName);
},
error: function (file, response) {
file.previewElement.classList.add("dz-error");
}
});
Dropzone.autoDiscover = false;
Dropzone.prototype.defaultOptions.dictRemoveFile = "Rimuovi file";
Dropzone.prototype.defaultOptions.dictCancelUpload = "Annulla";
这是处理上传的PHP脚本:
<?php
require_once '../db.config.php';
header('Content-Type: text/plain; charset=utf-8');
ini_set('upload_max_filesize', '200M');
ini_set('post_max_size', '201M');
ini_set('max_input_time', 320);
ini_set('memory_limit', '256M');
try {
if (
!isset($_FILES['file']['error']) ||
is_array($_FILES['file']['error'])
) {
throw new RuntimeException('Invalid parameters.');
}
switch ($_FILES['file']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
break;
case UPLOAD_ERR_INI_SIZE:
break;
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
break;
default:
throw new RuntimeException('Unknown errors.');
break;
}
// check filesize.
if ($_FILES['file']['size'] > 209715200) {
throw new RuntimeException('Exceeded filesize limit.');
}
// Check MIME Type.
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['file']['tmp_name']),
array(
'mp4' => 'video/mp4',
'mov' => 'video/mov',
'avi' => 'video/avi',
),
true
)) {
throw new RuntimeException('Invalid file format.');
}
// name uniquely.
$fileName = sha1_file($_FILES['file']['tmp_name']);
if (!move_uploaded_file($_FILES['file']['tmp_name'], sprintf('/var/www/html/beta.vedocompro.it/web/webtemp/%s.%s', $fileName, $ext ))) {
throw new RuntimeException('Failed to move uploaded file.');
}
try {
$PDO = new PDO('mysql:host=' . $DB_HOST . ';dbname=' . $DB_NAME,$DB_USER,$DB_PASS);
$insert = $PDO->prepare("INSERT INTO `videos` (`id`, `aid`, `accepted`, `uid`, `dir`) VALUES (NULL, '0', '0', '0', $fileName);");
$insert->execute();
echo $fileName;
} catch(PDOException $exception) {
echo $exception;
}
} catch (RuntimeException $e) {
echo $e->getMessage();
}
所以一切似乎都没问题,但服务器在出错之后断开连接(我认为与PDO
查询无关,导致2MB的较小文件将起作用。)
你能帮忙找出问题吗?
编辑做一些测试我发现脚本在执行30秒时完全丢失了,我尝试在脚本顶部添加set_time_limit(0);
但没有再改变< / p>
答案 0 :(得分:34)
XHR超时问题是指ajax呼叫配置。
要避免这种情况,必须在timeout: 180000
init参数中放置DropZone.js
(或您想要的ms)。
$("#dZUpload").dropzone({
url: "/ajax/admin/admin.acceptVideo.php",
maxFilesize: 209715200,
acceptedFiles: "video/*",
addRemoveLinks: true,
dataType: "HTML",
timeout: 180000,
success: function (file, response, data) {
// Do things on Success
},
error: function (file, response) {
file.previewElement.classList.add("dz-error");
}
});
使用30 seconds
上传文件时,这不会导致DropZone.js
超时。
<强>更新强>
正如@Brendon Muir报道的那样,你也可以0
插入timeout
来禁用超时。
DropZone.js文档报告默认超时为0,这是不正确的,默认超时为30秒。 值为0将禁用超时。
答案 1 :(得分:2)
我意识到这个问题有点老了,但是我在捕获超时行为方面遇到了问题,因此发现了此解决方案,而不是在发送函数中捕获超时,如下所示:
$("#dZUpload").dropzone({
url: "/ajax/admin/admin.acceptVideo.php",
maxFilesize: 209715200,
acceptedFiles: "video/*",
addRemoveLinks: true,
dataType: "HTML",
data: { id: '' },
success: function (file, response, data) {
var imgName = response;
file.previewElement.classList.add("dz-success");
$('#form_video').val(imgName);
},
error: function (file, response) {
file.previewElement.classList.add("dz-error");
},
//Called just before each file is sent
sending: function(file, xhr, formData) {
//Execute on case of timeout only
xhr.ontimeout = function(e) {
//Output timeout error message here
console.log('Server Timeout');
};
}
});