我正在尝试将“大”文件上传到我的应用程序。用户必须能够上传小于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);
,但没有再次更改 最佳答案
问题是XHR超时涉及到ajax调用配置。
为了避免这种情况,必须在timeout: 180000
初始化参数中放入DropZone.js
(或您想要的毫秒数)。
$("#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
以禁用超时。关于php - 上载30秒后,使用PHP上载Dropzone.js上载失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46379917/