本文介绍了PHP - 防止在上传文件期间上传超过 100 KB 的文件而不是在上传整个文件之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 php 代码的页面,可以为每个帐户上传用户的图片.他们的图片文件的大小必须小于 100 kb.

I have an page with php code to upload user's picture for every account.Size of their image file must less than 100 kb size.

我想防止用户在服务器上上传超过 100 KB 的文件(在图像配置文件字段中注册新用户)和上传期间(不是在上传整个文件之后,只是在上传期间),如果上传量超过 100 KB,则停止上传进度并通过 PHP(首选)或任何其他脚本语言用于服务器端代码向用户显示警告.

I want prevent uploading file more than 100 KB on server from users(in registering new user in image profile field) and during uploading(Not after uploading the entire file and just during uploading it), if the upload volume exceeds 100 KB, stop uploading progress and display warning for user by PHP(Preferred) OR any other script language for server-side code.

我通过 stackoverflow 和 google 搜索我没有找到任何帮助或来源.

I searched by stackoverflow and google I didn't find any help or source about that.

请帮帮我

推荐答案

最明显的选择是将几行 Javascript 添加到您的 HTML 中,这将是您的第一道防线:

The most obvious option is a few lines of Javascript added to your HTML that will be your first line of defense:

var uploadField = document.getElementById("file");

uploadField.onchange = function() {
    if(this.files[0].size > 100000){
       alert("File is too big!");
       this.value = "";
    };
};

如果您还确保您的表单只能通过这样的 javascript 提交:

If you then also ensure that your form can only be submitted via javascript like this:

http://javascript-coder.com/javascript-form/javascript-form-submit.phtml

这样,用户就不能只是关闭 Javascript 并上传他们想要的任何尺寸.

That way, the user can't just turn off Javascript and upload any size they want.

可能可以通过使用块进程将文件分块而不是一个大文件上传来做到这一点,但我认为我以前没有尝试过以这种方式限制文件大小...

It might be possible to do this by using a chunk process to upload your file in chunks instead of one big file, but I dont think I've tried to restrict filesize that way before...

这篇关于PHP - 防止在上传文件期间上传超过 100 KB 的文件而不是在上传整个文件之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 06:18