使用Malsup的jQuery文件上传进度栏在PHP中上传失败

前端之家收集整理的这篇文章主要介绍了使用Malsup的jQuery文件上传进度栏在PHP中上传失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 Malsup’s jQuery File Upload Progress Bar可视化进度条,同时将文件上传到服务器.现在我正在使用它.我在这个插件中有以下疑问:

1)文件上传即使进度条未达到100%

2)如果文件大小超过10 MB,则在进度条中加载高达100%,并返回上传失败!信息.

这是代码

<!doctype html>
<head>
<title>Uploads</title>
<style>
body { padding: 30px }
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }

.progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
.bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
.percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
    <h1>File Upload</h1>
    <form action="file.PHP" method="post" enctype="multipart/form-data">
        <input type="file" name="myfile[]" multiple><br>
        <input type="submit" value="Upload File to Server">
    </form>

    <div class="progress">
        <div class="bar"></div >
        <div class="percent">0%</div >
    </div>

    <div id="status"></div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
(function() {

var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');

$('form').ajaxForm({
    beforeSend: function() {
        status.empty();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },uploadProgress: function(event,position,total,percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
        //console.log(percentVal,total);
    },complete: function(xhr) {
        status.html(xhr.responseText);
    }
}); 

})();       
</script>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript"></script>
<script type="text/javascript">
_uacct = "UA-850242-2";
urchinTracker();
</script>

表单动作 – > file.PHP代码

<?PHP
$upload_directory = "../users/Files/";
if (move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_directory . $_FILES['userfile']['name'])) {
    print "<br><br>";
    print "<font color='black'>File Uploaded Successfully</font>";
    print "<br><br>";
    print "<font color='black'>Uploaded File is {$_FILES['userfile']['name']} and its size is {$_FILES['userfile']['size']} bytes </font>";
} else {
    print "Upload Failed!";
}
?>

我的服务器设置文件上传

post_max_size            1050M   1050M

upload_max_filesize  1050M   1050M

upload_tmp_dir           /tmp    /tmp

PHP.ini中的设置:

max_execution_time = 30 ; Maximum execution time of each script,in seconds

max_input_time = -1

memory_limit = 2000M ; Maximum amount of memory a script may consume (8MB)

解决方法

在您的PHP页面中,在PHP标签添加此行.
<?PHP
set_time_limit(0);

猜你在找的jQuery相关文章