Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

File Upload Progress Bar in PHP using jQuery

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

File Upload Progress Bar in PHP using jQuery

Postby highlander141 » Fri Nov 02, 2012 7:59 pm

I am using Malsup's jQuery File Upload Progress Bar http://www.malsup.com/jquery/form/progress2.html 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 :

Code: Select all
<!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 :
Code: Select all
<?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)
highlander141
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Nov 02, 2012 7:40 pm

Re: File Upload Progress Bar in PHP using jQuery

Postby johnj » Fri Nov 02, 2012 11:49 pm

Add these 2 lines at the top of your code to understand why it is not able to complete the upload.
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors', 'On');
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: File Upload Progress Bar in PHP using jQuery

Postby MeroD » Sat Nov 03, 2012 12:20 am

CSS3 Transition and HTML5 may give you better results as well:

.progress{ ..., transition:0.25s; }
.progress.done { width:100% !important; }

http://www.w3schools.com/css3/css3_transitions.asp
MeroD
New php-forum User
New php-forum User
 
Posts: 53
Joined: Wed Oct 10, 2012 12:14 am

Re: File Upload Progress Bar in PHP using jQuery

Postby highlander141 » Sat Nov 03, 2012 2:01 am

Hi Johnj,

the following is the error I get:

Notice: Undefined index: userfile in ../file.php on line 10

Notice: Undefined index: userfile in ../file.php on line 10

Thank you for your reply.. how can I solve this, is this the problem of POST ?
highlander141
New php-forum User
New php-forum User
 
Posts: 2
Joined: Fri Nov 02, 2012 7:40 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron