Tuesday, 30 April 2013

Tracking Upload Progress with PHP and JavaScript

Tracking Upload Progress with PHP and JavaScript


A problem that has plagued web developers for years is how to add real-time information to their applications, such as a progress bar for file uploads. Users are impatient; they don’t want to sit and wait while the browser is doing something and wonder whether it has frozen or if they have a slow connection. Providing a progress indicator gives users useful information and lets them know exactly what’s going on.
At first thought, you might think accomplishing this can be done easily by first obtaining the file’s size from the user’s computer and then performing some simple calculations against the directory on the server where the file is being uploaded to. On second thought, you’d find things aren’t quite that simple.
JavaScript can access a file’s name, type, and even the width and height of a local image, but it wasn’t until HTML5 that it could access a file’s size. Unfortunately, HTML5 still isn’t a completed standard yet and isn’t uniformly supported across all browsers. An alternate solution is to rely on a Flash, Java, or ActiveX plugin; no thanks, I’ll pass. Yet another solution is to install theAlternative PHP Cache extension, but that may not be available depending on your hosting environment and it seems like overkill for such a small task such as this.
It would seem as though all the options are fraught with nuisances and the task has quickly become a headache. But in the words of Yoda, “No… There is another.”
One of the many reasons I love PHP is that it makes seemingly difficult tasks easy. As of PHP 5.4, they’ve done it again with a new set of configuration directives, session.upload_progress.
In this article I’ll show you how this feature can be used to create a simple upload progress bar without any external libraries or browser dependencies. I’ll first discuss how it works, and then I’ll walk you through creating the four files needed to accomplish the task (an upload form, some JavaScript, a little CSS, and a file to return the status of the upload).

Session Upload Progress

Besides the usual requirements to allow file uploads, there are two more to track the progress. The session.upload_progress.enabled directive must be enabled and there must be a hidden field in your web form with the name specified by the session.upload_progress.namedirective. When session.upload_progress.enabled is true (as it is by default in PHP 5.4 and presumably beyond) and $_POST[session.upload_progress.name] is sent during an upload, information about the file transfer is made available in the $_SESSION superglobal array.
The print_r() output of the $_SESSION array will look similar to the following during a file transfer:
Array
(
    [upload_progress_myForm] => Array
        (
            [start_time] => 1323733740
            [content_length] => 721127769
            [bytes_processed] => 263178326
            [done] => 
            [files] => Array
                (
                    [0] => Array
                        (
                            [field_name] => userfile
                            [name] => ubuntu-10.04.3-desktop-i386.iso
                            [tmp_name] => 
                            [error] => 0
                            [done] => 
                            [start_time] => 1323733740
                            [bytes_processed] => 263178026
                        )
                )
        )
)
When you are developing either locally or on a fast network and upload small files, you won’t be able to visually observe the progress because the transfer happens so fast. In this case, you might want to try transferring a large file. Make sure the settings in your php.ini file allow large uploads, specifically the post_max_size and upload_max_filesize directives, and then verify they are sane values when you go to production.

Create the Form

The first file I’ll present is the upload form. Just to keep things as simple as possible, the example will post to itself and only handle one file upload at a time. Additionally, I won’t bother saving the file after it has been uploaded.
Here’s the code for form.php:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_FILES["userfile"])) {
    // move_uploaded_file()
}
?>
<html>
 <head>
  <title>File Upload Progress Bar</title>
  <link rel="stylesheet" type="text/css" href="style.css">
 </head>
 <body>
  <div id="bar_blank">
   <div id="bar_color"></div>
  </div>
  <div id="status"></div>
  <form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="POST" 
   id="myForm" enctype="multipart/form-data" target="hidden_iframe">
   <input type="hidden" value="myForm"
    name="<?php echo ini_get("session.upload_progress.name"); ?>">
   <input type="file" name="userfile"><br>
   <input type="submit" value="Start Upload">
  </form>
  <iframe id="hidden_iframe" name="hidden_iframe" src="about:blank"></iframe>
  <script type="text/javascript" src="script.js"></script>
 </body>
</html>

In the example the code to actually process the file has been omitted to keep things simple. If you’re interested in what such code should look like, check out the article File Uploads with PHPby Timothy Boronczyk.
After the head section which provides the page’s title and includes the stylesheet, you’ll notice a small collection of div elements. The div with the ID “bar_blank” is the container for the progress bar. The div with the ID “bar_color” will be dynamically updated as the file upload progresses. The “status” div will display the numeric value of the percent uploaded.
The form is set to submit to the same URL and its target attribute points to a hidden iframe element. Submitting a form to a hidden frame allows you to keep the visitor on the same page while the work is being done in the background. In fact, this is a common practice when doing “Ajax file uploads” since it isn’t possible to send the contents of a file directly using JavaScript’sXmlHttpRequest object.
Within the form, the special hidden field needed to populate the $_SESSION array appears, followed by a file upload input and submit button. Submitting the form will trigger a JavaScript function named startUpload() which will be defined by the included JavaScript file.
At the bottom of the page is the hidden frame to which the form will post and the import of thescript.js file.

Add Some Style

The next file, style.css, is pretty straight-forward. I’ve defined the size of the progress bar container and given it a 1px black border, the color of the progress bar as it’s loading, and both the iframe and the progress bar are hidden.
#bar_blank {
  border: solid 1px #000;
  height: 20px;
  width: 300px;
}

#bar_color {
  background-color: #006666;
  height: 20px;
  width: 0px;
}

#bar_blank, #hidden_iframe {
  display: none;
}

Client-Side Functionality

The script.js file is the largest of the group of files. It contains six functions which I will discuss below. Many people like to use jQuery to provide some of the functionality here, and you are certainly free to do so if you wish, but I personally prefer the old-school approach. Similar to how the Japanese place a higher value on hand crafted goods, I just feel more passionate about the code if it is my own.
function toggleBarVisibility() {
    var e = document.getElementById("bar_blank");
    e.style.display = (e.style.display == "block") ? "none" : "block";
}

function createRequestObject() {
    var http;
    if (navigator.appName == "Microsoft Internet Explorer") {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        http = new XMLHttpRequest();
    }
    return http;
}

function sendRequest() {
    var http = createRequestObject();
    http.open("GET", "progress.php");
    http.onreadystatechange = function () { handleResponse(http); };
    http.send(null);
}

function handleResponse(http) {
    var response;
    if (http.readyState == 4) {
        response = http.responseText;
        document.getElementById("bar_color").style.width = response + "%";
        document.getElementById("status").innerHTML = response + "%";

        if (response < 100) {
            setTimeout("sendRequest()", 1000);
        }
        else {
            toggleBarVisibility();
            document.getElementById("status").innerHTML = "Done.";
        }
    }
}

function startUpload() {
    toggleBarVisibility();
    setTimeout("sendRequest()", 1000);
}

(function () {
    document.getElementById("myForm").onsubmit = startUpload;
})();

The toggleBarVisibility() function sets an appropriate style on the “bar_blank” div to show or hide the progress bar as needed. Initially it starts out hidden, but will be displayed once an upload starts and hidden again when an upload finishes.
The createRequestObject() function creates an XMLHttpRequest or ActiveXObject object based on the user’s browser. This is probably the function most people would look to jQuery or some other JavaScript framework to provide.
The sendRequest() function requests the progress.php file with a GET request, and then invokes the handleResponse() function to handle the returned data.
The handleResponse() function handles the response from progress.php which will be a number between 1-100 depending on the file upload progress. I also update the “status” div with the appropriate value. If the current percent is below 100 then I call JavaScript’s nativesetTimeout() function to send another request for an update after 1 second (you may want to adjust this value as appropriate), otherwise I hide the progress bar again and set the status to “Done.”
The startUpload() function makes the upload bar visible and sends a request for an update after a delay of 1 second. This small delay is needed in order to give the upload time to start.
The final function is a self-executing anonymous function which registers startUpload() with the form’s submit event.

Real-Time Progress

The last file that brings everything together is the progress.php file:
<?php
session_start();

$key = ini_get("session.upload_progress.prefix") . "myForm";
if (!empty($_SESSION[$key])) {
    $current = $_SESSION[$key]["bytes_processed"];
    $total = $_SESSION[$key]["content_length"];
    echo $current < $total ? ceil($current / $total * 100) : 100;
}
else {
    echo 100;
}
?>

The script performs some simple math on the current number of bytes transferred divided by the total file size, multiplied by 100 and rounded off to give a percent.
Information about the transfer is keyed with a concatenation of the session.upload_progress.prefix directive’s value and the hidden session.upload_progress.name field’s value. Because my form passed “myForm”, the session’s key is determined withini_get("session.upload_progress.prefix") . "myForm".
Here’s a screenshot of the progress bar in action:
upload progress bar

Fine-Tuning the Behavior

PHP provides some additional directives to help fine-tune the behavior of session uploads you should be aware of. For example, session.upload_progress.cleanup, which is set to 1 by default, cleans up the extra session data immediately after the upload has finished. You need to be careful to avoid a potential race condition.
Take a look again at the code in progress.php and you’ll notice that I check to see if$_SESSION[$key] is empty or not before proceeding. My JavaScript functions fire off every second as long as the result returned from progress.php is less than 100. Ifsession.upload_progress.cleanup is enabled and my script fetches 99% of the upload and a 1/2-second later the upload finishes, $_SESSION[$key] would not exist for the next check. If I didn’t take that into consideration then my JavaScript function might keep firing, even after the upload finished.
Another two directives are session.upload_progress.freq andsession.upload_progress.min_freq which both determine how often the session should be updated. The value of freq can be given in either bytes (i.e., 100) or the percentage of total bytes (i.e., 2%). The value of min_freq is given in seconds and indicates the minimum number of seconds between updates. Obviously if min_freq was set to update every 1 second, it would be pointless for your JavaScript to check every 100 milliseconds.

Summary

You should now have a solid grasp on how to create a progress bar for file uploads using the session upload progress feature. Moving forward, I encourage you to experiment with uploading multiple files, giving the option to cancel an upload in progress using $_SESSION[$key]["cancel_upload"], or whatever other ideas your mind can muster. Please share in the comments your experiences and improvements you’ve made for others to benefit from and enjoy as well!

PHP 5.4 is Released — What’s New?


It’s difficult to believe almost three years have elapsed since PHP 5.3.0. The next version should have been PHP 6.0 but unicode problems have delayed development. This latest version provides many of the features planned for version 6.0.
PHP 5.4 is available to download from the php.net website. There’s a PHP 5.3 migration guide if you want to keep your old settings. While it’s stable, you’d be advised to test your sites and applications before installing it on live servers. The PHP team often release a bug-fix version a few weeks after the initial release.
So let’s look at the best new features and improvements…

Short Array Syntax

It’s now possible to use finger-saving JavaScript-like square brackets rather than using the old array(…)construct, e.g.

$array1 = [1, 2, 3];
$array2 = [
"one" => "first",
"two" => "second",
"three" => "third"
];

Traits

Traits reduce some limitations of single inheritance. In essence, traits are similar to abstract classes and can contain any number of properties and methods. A class can then use any number of traits, e.g.


trait Hello
{
function sayHello() {
return "Hello";
}
}
trait World
{
function sayWorld() {
return "World";
}
}
class MyWorld
{
use Hello, World;
}
$world = new MyWorld();
echo $world->sayHello() . ' ' . $world->sayWorld();
For more information, refer to Using Traits in PHP 5.4 on PHPmaster.com.

Built-in Web Server

PHP 5.4 offers a built-in web server which runs from the Windows, Mac or Linux command line. While it’s not Apache or IIS, it’s fine for simple testing. I suspect many of the better PHP IDEs will implement support shortly.
For more information, refer to PHP 5.4′s New Built-in Web Server.

Saturday, 27 April 2013

PHP MySQL Online Training




Web Development with PHP and MySQL Training

Client Side Scripts:
  1.  HTML
  2.  CSS
  3.  Java Script
  4.  Jquery

Server Side Scripts:
  1.   PHP
Data Base:
  1.  MySQL with phpmyadmin

Summary:
This online training series teaches learners to create dynamic Web sites. Learners will be able to apply their knowledge to the creation of dynamic Web applications such as content management, user registration, and e-commerce.

This PHP and MySQL online training series provides 43 hours of self-paced courseware. This web-based training provides 1 year of unlimited access to all of the following lessons:
HTML Lessons:
After taking this course, the learner will be able to create write HTML forms, tables and HTML pages
  • Introduction to html
  • What is an html?
  • Introduction to editors and browsers
  • What is tag?
  • Discuss about basic tags of html
  • Discuss about head section tags
  • What is an attribute?
  • Discuss about an attribute
  • Discuss about text formatting tags
  • Discuss about body tags(marquee, img, embed, anchor, named anchor)
  • Introduction to tables
  • Template Creation with tables
  • Lists
  • Introduction to forms
  • Discuss about forms
  • Basic Web Page Creation using HTML
We will give html training completely in notepad
CSS
  • Introduction to css
  • What is css
  • Types of css
  • What is inline css
  • Discuss inline css
  • What is internal css
  • Discuss internal css
  • Discuss about properties
  • Types of selector
  • Difference  b/w class selector and id selector
  • What is div
  • Template creation with divs
  • Difference b/w div and span
  • External css
  • Complete Web Template Creation using HTML and CSS
Introduction Dreamweaver IDE Tool
·         Introduction Adobe Dreamweaver
·         Dreamweaver Interface
·         Site Creation
·         Web Template Creation in Dreamweaver 
JavaScript 
After taking this course, the learner will be able to create write HTML web pages with java script validations.
  • Introduction JavaScript
  • What is JavaScript
  • What is statement
  • Types of statements
  • What is variable
  • Variable declaration
  • What is an operator
  • Types of operators
  • Discuss operators theoretically and practically
  • Conditional statements
  • Introduction to loops
  • For loop examples
  • While loop examples
  • Difference b/w while and dowhile loops
  • functions
    • Parameterized functions
    • Non-parameterized functions
    • Recursive functions
    • Difference b/w parameterized and non-parameterized functions
    • Math calculations using functions
    • Math calculations using functions and forms
  • Introduction to objects
  • What is property and method
  • String object
  • Math object
  • Navigator object
  • screen object
  • Date  object
  • Array object
  • Introduction to HTMLDOM
  • Document object
  • Window object
  • Discuss dropdown menu practically
  • Discuss navigation menu practically
For each object we will discuss properties and methods with examples in Dreamweaver EditorPHP 
Lesson Detail:

Installing Required Software

After taking this course, the learner will be able to download, install, configure, and test all the software required to create dynamic Web sites using PHP and MySQL.

  • Installing MySQL
  • Installing Apache with PHP
  • Setting MySQL permissions
  • Testing the installation
  • Configuring PHP


          Discuss about IDE Tools

  Discuss about Application Servers and Database Servers
  • Introduction to php
  • What is php
  • Installing Required Softwares 
  • Installation process of XAMPP
  • How to write php page
  • Php output statement and method
  • Where to  save php page
  • How to run php page
  • Php variables
  • Php operators
  • Php variable methods
  • Conditional statements
  • Loops
  • Functions
  • Date and time functions
  • Arrays
    • What is an array
    • Discuss about single dimensional array
    • Discuss about two-dimensional array
    • Discuss about multi-dimensional array
    • Array  methods
    • Array sorting  methods
    • For each as loop
    • While list each loop
    • Difference b/w explode and implode methods
    • For in loop
    • Built-in Arrays
MySQL
  • Introduction to MySQL
  • Choosing column types
  • Choosing other column properties
  • Using mysql client
  • Creating databases and tables
  • Inserting records
  • Selecting data
  • Using conditionals
  • Using LIKE and NOT LIKE
  • Sorting and limiting query results
  • Updating and deleting data
  • Using text and numeric functions
  • Formatting date and time functions
  • String handling
  • Regular expressions(patterns)
  • Send data from client to server with form validation
  • File handling
  • File uploading
  • QueryString
  • Setting cookies
  • Accessing cookies
  • Setting cookie parameters
  • Deleting cookies
  • Setting session variables
  • Accessing session variables
  • Deleting session variables
  • Changing session behavior
  • Changing the session cookie settings
  • Using sessions without cookies
  • Improving session security
  • Login page creation with remember me (sessions and cookies)
  • Ajax
    • Introduction to Ajax
    • What is XMLHTTP Request object?
    • Ajax properties
    • Ajax methods
    • Ajax event handler
    • Ajax with php and mysql
    • Double drop down list boxe
  • Introduction to php oops
    • What is class
    • What is an object
    • What is constructor
    • What is property
    • What is method
    • What is access specifiers
    • Inheritance
    • Method overloading
    • Method overriding
    • Parameterized and non-parameterized constructors
    • Polymorphism
    • Interface
    • oops examples
    • Static
    • Final
    • Abstract
  • Admin login creation
  • Admin panel creation
  • Date and time functions
  • Mail
  • Introduction to jquery
    • What is jquery
    • Introduction to Jquery selectors
    • Introduction  to jquery filters
    • Types of jquery filters
    • Jqery animations and effects
    • Jquery events and functions
    • jqueryUI
    • jquery with ajax
  • php with jquery examples
Web Application Security

In this course, the learner will become familiar with methods for increasing the security of their web applications using PHP and MySQL. These techniques include methods of form validation, the ability to define patterns and then matching or replacing those patterns, and data encryption.


  • Preventing multiple submissions
  • Validating the right form
  • Handling HTML
  • Validating data
  • JavaScript form validation
  • Defining a pattern with regular expressions
  • Matching regular expression patterns
  • Matching and replacing patterns
  • Database security and encryption
  • Secure salt storage
Content Management Example

After taking this course, the learner will become familiar with the foundational information required to build a sample content management site through PHP interactions using a database.

  • Creating the template
  • Creating the basic pages
  • Managing URLs
  • The list() function
  • The serialize() and urlencode() functions
  • Managing files
  • Viewing and downloading files

User Registration Example

This course provides an overview of the user registration system.

  • Creating the templates
  • Writing the configuration scripts
  • Creating the home page
  • Registration
  • Activating an account
  • Logging in and logging out
  • Resetting a password
  • Changing a password
ü  Creating Complete Dynamic Website Using PHP and MySQL
ü  Domain Creation
ü  Website Hosting

Certification: 

PHP and MySQL



************************************************************

For Registration 
************************************************************