使用Malsup';在PHP中上载失败;s jQuery文件上载进度条

使用Malsup';在PHP中上载失败;s jQuery文件上载进度条,php,jquery,jquery-forms-plugin,Php,Jquery,Jquery Forms Plugin,我在上传文件到服务器时使用可视化进度条。我现在正在使用它。我对此插件有以下疑问: 1) 即使进度条未达到100%,也会上载文件 2) 如果文件大小超过10 MB,它会在进度条中加载高达100%,并回显上载失败!信息 代码如下: <!doctype html> <head> <title>Uploads</title> <style> body { padding: 30px } form { display: block; margin

我在上传文件到服务器时使用可视化进度条。我现在正在使用它。我对此插件有以下疑问:

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, position, 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>
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);

php.ini
中有一个最大大小限制,我为此替换了
文件.php

<?php
set_time_limit(0);
$allowedExts = array("jpg", "jpeg", "gif", "png", "rar", "zip", "tgz", "iso", "gz",         "img", "exe", );
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "application/rar")
|| ($_FILES["file"]["type"] == "application/octet-stream")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000000000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("/var/www/file/upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "/var/www/file/upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "/file/upload/" . $_FILES["file"]["name"];

     }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

文件名中有错误…即:

<input type="file" name="myfile[]" multiple><br>

尽管仍有一些错误

如果它真的要抱怨上传失败,我不确定它是否超时……30秒后,php脚本将停止,即使它没有上传完整的文件,并将响应返回到失败的ajax函数。@AbhishekSaha,谢谢你的回复。我添加了这个标签,即使文件大小限制超过10MB,它也显示上载失败!消息..我查看了演示。它的马车。进度条工作不正常。从chrome测试。它工作良好,我喜欢它,因为它简单,没有任何Flash等,我无法理解它,因为这是服务器或jQuery脚本的问题!?这些脚本可能有一些限制文件上载大小限制的代码吗?我查过密码,但没找到。。
<?php
set_time_limit(0);
$allowedExts = array("jpg", "jpeg", "gif", "png", "rar", "zip", "tgz", "iso", "gz",         "img", "exe", );
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "application/rar")
|| ($_FILES["file"]["type"] == "application/octet-stream")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 200000000000000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("/var/www/file/upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "/var/www/file/upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "/file/upload/" . $_FILES["file"]["name"];

     }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
<input type="file" name="myfile[]" multiple><br>
<input type="file" name="file" multiple><br>