Wednesday 25 December 2013

How to get file extension in PHP

In PHP there are many ways to exclude a file extension such as using substr() or RegEx etc. But to use pathinfo() is the better way to deal with this.

The pathinfo() function returns an array that contains information about a path. The following array elements are returned:

Array
(
[dirname] => /directory_name (Ex. Test)
[basename] => file name (Ex. mydoc.png)
[extension] => file extension (Ex. png)
)

It has two parameters: pathinfo(path,options);
path: filepath
options:
PATHINFO_DIRNAME - return only dirname
PATHINFO_BASENAME - return only basename
PATHINFO_EXTENSION - return only extension

<?php

print_r(pathinfo("myfile.png",PATHINFO_EXTENSION));

Output: png

?>

Monday 23 December 2013

Show Uploaded Image Preview using JQuery


<style>
    img{
        border: 1px solid #000; 
        padding: 2px;
        border-radius: 5px;
        background: #FFF;
    }
</style>


<input type="file" name="myimage" id="myimage">
<img id="preview" alt="View Image" width="250px" height="250px"/>


<script type="text/javascript">
    $("#myimage").change(function() {
        if (this.files && this.files[0]) {            
            var reader = new FileReader();
            reader.onload = function(e) {
                $('#preview').attr('src', e.target.result);
            }
            reader.readAsDataURL(this.files[0]);
        }
    });
</script>

Demo

Sunday 8 December 2013

Pan card validation using JQuery Validate

PAN card number is a unique national number issued in India for tax related purposes.

Add jquery.js & jquery.validate Plugin

<script type="text/javascript" src="/js/jquery.js"></script>

<script type="text/javascript" src="/js/jquery.validate.js"></script>

Create HTML Form

<form action="action_name" method="POST" id='myform'>
<input type='text' name='pan'>
<input type='submit' value='Submit' name='submit'>
</form>

JQuery Validate Code

<script type='text/javascript'>
   $.validator.addMethod("pan", function(value, element)
    {
        return this.optional(element) || /^[A-Z]{5}\d{4}[A-Z]{1}$/.test(value);
    }, "Invalid Pan Number");


$("#myform").validate({
        rules: {
            "pan": {pan: true},
        },
    });
</script>