当上传超出PHP设置允许的最大大小的文件时,通常会在我能够处理来自服务器端的响应之前上传整个文件。
在php.ini中,我将最大帖子大小和最大上传大小设置为2 MB。当我在上传表单中选择超过2 MB的文件(也称为1 GB文件)时,服务器仍然接受该文件,但我需要等到整个文件上传才能处理/处理上传事件
现在,有趣的部分,是上传进度会话返回100%,允许我返回上传表单的控件(隐藏进度条,显示上传表单和再次提交按钮),但由于必须等待上传实际完成我无法将回复返回给用户。
对于我的项目,有3个部分:
问题是如果上传太大,#3显示为完整,但#2继续接受数据,直到整个文件上传为止。
我想知道的是,有什么方法可以立即检测到这种情况,当它发生时不会100%依赖JavaScript来检查上传前的文件大小,因为这可以被绕过。我知道我可以在文件发送之前检查客户端,但是我想在文件过大之前在javascript中实现客户端中止之前实现适当的服务器端上传。这意味着,我不想依赖JavaScript来保证服务器安全(文件类型,大小等)。
希望我能很好地解释一切,以便理解。我已经在PHP中看到了这个问题多年了,当数据接收阈值超过php.ini中设置的限制时,我很惊讶文件上传在服务器端没有自动中止
如果它有所不同,这是我的PHP版本详细信息
PHP 5.6.17-0+deb8u1 (cli) (built: Jan 13 2016 09:10:12)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
PHP FPM(在我的apache配置中提供php服务)
PHP 5.6.17-0+deb8u1 (fpm-fcgi) (built: Jan 13 2016 09:10:13)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with the ionCube PHP Loader (enabled) + Intrusion Protection from ioncube24.com (unconfigured) v5.0.23, Copyright (c) 2002-2016, by ionCube Ltd.
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
和Apache(根据要求)
Server version: Apache/2.4.10 (Debian)
Server built: Nov 28 2015 14:05:48
Server's Module Magic Number: 20120211:37
Server loaded: APR 1.5.1, APR-UTIL 1.5.4
Compiled using: APR 1.5.1, APR-UTIL 1.5.4
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_SYSVSEM_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/apache2"
-D SUEXEC_BIN="/usr/lib/apache2/suexec"
-D DEFAULT_PIDLOG="/var/run/apache2.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="mime.types"
-D SERVER_CONFIG_FILE="apache2.conf"
答案 0 :(得分:2)
使用Javascript并在上传之前检查文件大小,但这不是关于如何从服务器端中止中上传的示例。
这是一个实例:http://codesheet.org/codesheet/E8ZwGzoV
<form method="post" target="submit" enctype="multipart/form-data" action="/post.php">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
<div id="message"></div>
<iframe name="submit" width="100%" height="400px"></iframe>
JS在这里:
document.forms[0].addEventListener('submit', function( evt ) {
var file = document.getElementById('file').files[0];
// 1 MB = 1048576 this size is in bytes
if(file && file.size < 2097152) { // 2MB = 2097152
//Submit form
document.getElementById('message').innerHTML = 'The File '+bytesToSize(file.size)+' is OK!';
}
else
{
//Prevent default and display error
document.getElementById('message').innerHTML = 'The File '+bytesToSize(file.size)+' is to big!';
evt.preventDefault();
}
}, false);
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 Bytes';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}