Upload fails in PHP using Malsup's jQuery File Upload Progress Bar
Asked Answered
F

3

10

I am using Malsup's jQuery File Upload Progress Bar for visualizing progress bar while uploading files to server. I am using it now at present. I have got the following doubts in this plugin:

1) File uploads even if the progress bar has not achieved 100%

2) If the File size is more than 10 MB, it loads upto 100% in the progress bar and echoes Upload failed! message.

Here is the code :

<!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>

The form action -> file.php code :

<?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!";
}
?>

My server settings for File Uploads:

post_max_size            1050M   1050M

upload_max_filesize  1050M   1050M

upload_tmp_dir           /tmp    /tmp

Settings in 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)
Fipple answered 2/11, 2012 at 13:1 Comment(0)
S
1

In your php page, add this line after php tag.

<?php
set_time_limit(0);
Stockton answered 2/11, 2012 at 13:5 Comment(7)
If it's actually getting to the point of complaining about the upload failing, I'm not sure it's timing out...Kanal
After 30 secs, php script will stop even if it has not uploaded the complete file and echo the response back to the ajax function that it failed.Stockton
@AbhishekSaha, thank you for the reply. I have added this tag, even though if the file size limit is more than 10 MB, it is showing Upload Failed! message..Fipple
I checked the demo. Its buggy. Progress bar doesnt work properly. Tested from chrome.Stockton
Its working good, I liked it because, its simple without any Flash etc, I am not able to figure it out, as is it a problem of Server or jQuery script!? Can it be possible that these scripts might have some code which restricts the file upload size limit? I have checked the code but didn't find any..Fipple
Yes, that is a possibility. I am not getting the proper documentation for this script. Send me the link if you know.Stockton
let us continue this discussion in chatFipple
P
1

In php.ini there is a max size limit and I replaced the file.php for this:

<?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";
  }
?>
Pentup answered 21/1, 2013 at 7:36 Comment(0)
K
1

You have an error in the name of the file...i.e:

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

While u are accessing it in php code as $_FILES["file"]["type"]. Change the name to file instead of myfile[]. Change to :

<input type="file" name="file" multiple><br>

There are still some more errors though

Kreit answered 5/4, 2013 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.