doudi5291 2015-10-27 08:48
浏览 35

为IE9 / PHP / jQuery强制高效的小文件上传

I want to have a small upload limit (e.g. 100 kb for testing, perhaps 6 Mb ultimately). The size of the upload can be checked:

  1. before the upload

  2. fail when too much is uploaded

  3. after the upload

If the user is trying a 1 Gb file ideally (1) should happen so that the file isn't uploaded at all. If not, (2) should happen so that it doesn't take long before the user knows the file is too big. I'd like to avoid the other possibility (3).

In HTML5 but not in IE9 the filesize can be checked before uploading using:

this.files[0].size

Get file size before uploading

In IE9 the following might work if the security settings are adjusted:

var objFSO = new ActiveXObject("Scripting.FileSystemObject"); var filePath = $("#" + fileid)[0].value;
var objFile = objFSO.getFile(filePath);
var fileSize = objFile.size; //size in kb

Ideally I'd like to use a method that works with IE9. I've heard about flash files being used. I'd like a method that is separate - not using a big plugin.

Here is my code. At the moment it uploads the whole file before it checks if the file size is too big.

<?php
if (isset($_FILES['myfile'])) {
    if ($_FILES['myfile']['error'] == UPLOAD_ERR_FORM_SIZE) {
        // $_FILES['myfile']['size'] is 0
        echo 'Error: file is too big!<br>';
    }
    else if ($_FILES['myfile']['size'] > 100000) {
        echo 'File size is too big!<br>';
    }
    else {
        echo 'File uploaded ok.<br>';
    }
}
var_dump($_FILES);
?>
<form method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    <input type="file" name="myfile" />
    <input type="submit" value="Submit" />
</form>

The small upload limit is just for one form so I don't want to change the global PHP settings file.

I'm not sure using MAX_FILE_SIZE is a good idea - the person still has to upload the entire file and the filesize data is lost (I might want to tell people how big their upload was)

ini_get('post_max_size') returns 8M and ini_get('upload_max_filesize') returns 32M. If files that are over 8 Mb try to be uploaded the file is still fully uploaded (even 40+ Mb). After larger than 8 Mb files are uploaded var_dump($_FILES) returns an empty array.

If the file is bigger than about 600 Mb I get a 413 error... that is (1).

So I want to do (1) or (2) in IE9, PHP and jQuery.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试,帮帮忙吧