Friday, 17 June 2016

How to get the file extension using PHP

Question:

How can I grab the extension of a file I have uploaded using PHP?

Answer:

PHP comes with a built-in function called ‘pathinfo()’ which allows you to get the extension of file you have uploaded.
$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
echo $ext;

Difference between double and triple equal to operators in PHP

This is one of the most common interview questions in PHP. Both == and === are comparison operators which are used to compare two values. But their is a major difference between the two. The ‘==’ (double equal to) compares and test if the two values on left and right side are equal. On the other hand, ‘===’ (triple equal to) tests if the two values are equal as well as check if they are of same data type. Let me explain you with help of an example,
<?php
$x = 100; $y = "100";
if($x == $y){
 echo "Yes";
}else{
 echo "No";
}
?>
The output of above code will be “YES” as  “==” operator compares only values and values in $x and $y are same.
<?php
$x = 100; $y = "100";
if($x === $y){
 echo "Yes";
}else{
 echo "No";
}
?>
The output of above code will be “NO” as even the values in $x and $y are same, there data type is different.

How to Get the Current Page URL in PHP?

When developing PHP based web application,  sometimes you need to grab the URL of current page.  Using the following PHP code snippet you will be able to get the URL of current webpage.
<?php
/**
 * Grabs and returns the URL of current page.
 * @param   none
 * @return  URL of current page
 */
function grabCurrentURL(){
 if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
  $url = "https://";
 }else{
  $url = "http://";
 }
 $url .= $_SERVER['SERVER_NAME'];
 if($_SERVER['SERVER_PORT'] != 80){
  $url .= ":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 }else{
  $url .= $_SERVER["REQUEST_URI"]; 
 }
 return $url;
}

echo grabCurrentURL();
?>

Simple PHP Password Generator

When building web based applications, often you need to generate passwords for users. This is a simple PHP script which allows you to generate random passwords.
<?php
/**
 * Generates random passwords.
 * @param   int  Password Length (Default : 10)
 * @return  Random String
 * @author  Harshal Limaye (http://coffeecupweb.com/)
 */
function genPass($len = 10) {
    $charPool = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    $pass = array();
    $length = strlen($charPool) - 1;
    for ($i = 0; $i < $len; $i++) {
        $n = rand(0, $length);
        $pass[] = $charPool[$n];
    }
    return implode($pass);
}
echo genPass();
?>

How to extract Zip Files with PHP?

In this post, I will show you, how to extract zip/compressed files using PHP. PHP has built-in extensions for dealing with compressed files.
Following PHP function can be used to unzip the compressed files. It accepts two parameters, source i.e full path to zip file and destination i.e path to which files will be extracted.
<?php
//extract a file with php
function extractZip($src,$dest){
 $zip = new ZipArchive;
 $res = $zip->open($src);
 if ($res === TRUE) {
  $zip->extractTo($dest);
  $zip->close();
  echo 'Files Extracted Successfully!';
 } else {
  echo 'Extraction Failed!';
 }
}

$src = "zip/file.zip";
$dest = "extractedfile/1/";
echo extractZip($src,$dest);
?>

How to create a zip file in php?

In one of my previous post, We  have already seen How we can extract Zip Files using PHP. In this tutorial, we are going to create a Zip file with PHP. To perform this task, we are going to use a built-in extension in PHP known as ZipArchive classs.
This is a very basic PHP function which accepts two parameters array of files to be zipped and name of zip file to be created.
//Creating a Zip File Using PHP
function genZip($files = array(),$zipName){
 $zip = new ZipArchive();
 $zip->open($zipName.'.zip', ZipArchive::CREATE);
 foreach($files as $file){
  $zip->addFile($file);
 }
 $zip->close();
}
Usage:
//Usage of genZip function
$files = array(
  'file1.pdf',
  'file2.pdf',
  'file3.pdf',
  'folder2/file4.pdf',
  'folder2/file5.pdf'
 );
$zipName = 'myfiles';
genZip($files,$zipName);