Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
How to delete the entire directory with in its files in php

How to delete the entire directory with in its files in php

Generally we use the remove directory rmdir() method. In this case the directory will be removed when it has no files on it.
another way is unlink() method. This method can be used for removing the single file from the directory location.

How to download the multiple directories as a zip file in PHP

If you want to remove the directory and its all files and sub directories.
First removes the sub directories and its files after that removes the main directory. This is called as the recursion.
 <?php  
  function deleteDir($dirPath) {  
   if (! is_dir($dirPath)) {  
     throw new InvalidArgumentException("$dirPath must be a directory");  
   }  
   if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {  
     $dirPath .= '/';  
   }  
   $files = glob($dirPath . '*', GLOB_MARK);  
   foreach ($files as $file) {  
     if (is_dir($file)) {  
       deleteDir($file);  
     } else {  
       unlink($file);  
     }  
   }  
   rmdir($dirPath);  
 }  
 deleteDir("Directoryname");  
 ?>  

* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff
How to get the user ip address in php

How to get the user ip address in php

In many situations developers want's the user ip address.

Those are
In user registration time store his/her ip address.
In login time also store his/her ip address .

In these two factors developer compares the registered ip address and logged ip address. If both are equal no issue when these are not equal then intimate the user "You are logined the unknown ip address".

In other situation when developers want's to block the some of the users ip.
Because of user voilating the site rules webmaster can restrict the access to the perticular ip address.

Some situations completely block the site access to the particular ip address.

This is a simple script to get the users ip address using the php.
 
<?php  
  $client = @$_SERVER['HTTP_CLIENT_IP'];  
  $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];  
  $remote = @$_SERVER['REMOTE_ADDR'];  
  if(filter_var($client, FILTER_VALIDATE_IP)){  
  $ip = $client;  
  }elseif(filter_var($forward, FILTER_VALIDATE_IP)){  
  $ip = $forward;  
  }else{  
  $ip = $remote;  
  }  
 echo $ip;  
?>

* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff
Live html compiler using php and bootstrap

Live html compiler using php and bootstrap

If you want to make the live html compiler similar to the w3schools.com try editor

What is live html compiler ?
you can see the exact output of the html code before save and run in the browser.

This is a simple technique to develop the live html code in the php.
Here we are going to use the textarea as a input of the html code and iframe as a output of the the input code.

in left textarea give the input of the html code and click the see results button then it renders the html code preview in the right iframe instantly.

index.html
 
 <title> Live html compiler using Php and bootstrap</title>  
 <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">  
 <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>  
 <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>  
 <style type="text/css">  
 iframe { border: 1px silver solid; }  
 textarea { resize:none; }  
 .glyphicon{font-size: 25px;}  
 </style>  
 <div class="container" >  
 <div class="row">  
 <div class="col-md-6">  
 <h2><span class="glyphicon glyphicon-align-justify"></span> Html Code </h2>  
 <form id="htmlcomplier" name="htmlcomplier" action="result.php" target="view" method="post">  
 <div class="form-group">  
 <textarea class="form-control" required rows="24" id="code" Placeholder="Enter your code and Submit" name="code">  
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
  <title>Bootstrap Example</title>  
  <meta charset="utf-8">  
  <meta name="viewport" content="width=device-width, initial-scale=1">  
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">  
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>  
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
 </head>  
 <body>  
 <div class="container">  
  <div class="jumbotron">  
   <h1>Demo</h1>  
   <p>Demo tag line!</p>  
  </div>  
  <div class="row">  
   <div class="col-sm-4">  
    <h3>Column 1</h3>  
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>  
    <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...</p>  
   </div>  
   <div class="col-sm-4">  
    <h3>Column 2</h3>  
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>  
    <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...</p>  
   </div>  
   <div class="col-sm-4">  
    <h3>Column 3</h3>  
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</p>  
    <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...</p>  
   </div>  
  </div>  
 </div>  
 </body>  
 </html>  
 </textarea>   
 <br/>  
 <input class="btn btn-success pull-right" type="submit" value="See result" />  
 </div>  
 </form>  
 </div>  
 <div class="col-md-6">  
 <h2><span class="glyphicon glyphicon-list-alt"></span> Result </h2>  
 <div class="form-group">  
 <iframe width="100%" height="510px" frameborder="0" src="result.php" name="view" id="view"></iframe>  
 </div>  
 </div>  
 </div>  
 </div>  

result.php
 
<?php  
$Getcode = @$_REQUEST["code"];  
print $Getcode ;  
?>  

Download Demo
* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff
How to auto load the page in div every 5 seconds

How to auto load the page in div every 5 seconds

Now a days we are seen many websites are fetch the data without page refresh.
If you want to load the page auto  in div by using jquery. It is possible by using the setInterval() here you can mention the auto load page and time.
Then you are defined the page will load the certain amount of time as you set in the function.
This page content will be load in a div.
Please wait ..
Then set the auto load page and time interval in setInterval().

You need to load the jquery.min.js file.

Your auto load page contains either static data or dynamic data.
index.php
 <html>  
 <head>  
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>  
 <script>  
 setInterval(  
 function()  
 {  
 $('#content').load('load.php');  
 }, 3000);  
 </script>  
 <style>  
 #content{  
 background-color:#00A1E0;  
 font-size:24px;  
 font-weight:bold;  
 padding-top:10px;  
 color:#fff;  
 min-height: 200px;  
 }  
 #content,h1{  
      text-align: center;  
 }  
 </style>  
 <title>Auto Load Page in Div using Jquery</title>  
 </head>  
 <body>  
 <h1>Auto Load Page in Div</h1>  
 <div id="content"> Please wait .. </div>  
 </body>  
 <html> 
load.php
 <?php  
 echo 'This content is loaded via ajax in every 5 seconds ..';  
 ?>

Download Watch Demo
* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff
How to download the multiple directories as a zip file in PHP

How to download the multiple directories as a zip file in PHP

If you want zip the multiple directories and make available to download that zip file. You can use the ZipArchive class to create a ZIP file and stream it to the client.
You can write like this
 $za = new ZipArchive;
$res = $za->open($zip_file_name, ZipArchive::CREATE);
if($res === TRUE) 
{
    foreach ($the_folder as $key => $value)
    {
        $za->addDir($value, basename($value));
    }
    $za->close();
}
else  
{ 
    echo 'Could not create a zip archive';
}
Here $the_folder is a array it contains the directories list. Those are able to add the zip file by using this
 
$za->addDir($value, basename($value));
in case of adding / making the zip file any error occures it shows the error message
 
echo 'Could not create a zip archive';
To initialize the directories list like this
 
$the_folder =['pictures/','movies/'];
Here you simply mention the path of the directories.
And then to stream it.
 
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: " . filesize($zip_file_name));
    readfile($zip_file_name);
The sixth line forces the browser to present a download box to the user and prompts the name filename.zip. The eight line is optional but certain (mainly older) browsers have issues in certain cases without the content size being specified.
After download you can remove the zipped folder from your server
 
if($delete_file_after_download)
{
    unlink($zip_file_name);
}?>
Full code
 
 <!--  
 * @author TechiesBadi  
 * How to download the multiple directories as a zip file  
 -->  
 <?php  
 $the_folder =['pictures/','movies/'];  
 $zip_file_name = 'download.zip';  
 $download_file= true;  
 $delete_file_after_download= true;   
 class FlxZipArchive extends ZipArchive {  
   /** Add a Dir with Files and Subdirs to the archive **/  
   public function addDir($location, $name) {  
     $this->addEmptyDir($name);  
     $this->addDirDo($location, $name);  
    } // EO addDir;  
   /** Add Files & Dirs to archive **/  
   private function addDirDo($location, $name) {  
     $name .= '/';  
     $location .= '/';  
     // Read all Files in Dir  
     $dir = opendir ($location);  
     while ($file = readdir($dir))  
     {  
       if ($file == '.' || $file == '..') continue;  
       // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();  
       $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';  
       $this->$do($location . $file, $name . $file);  
     }  
   } // EO addDirDo();  
 }  
 $za = new FlxZipArchive;  
 $res = $za->open($zip_file_name, ZipArchive::CREATE);  
 if($res === TRUE)   
 {  
   foreach ($the_folder as $key => $value)  
   {  
     $za->addDir($value, basename($value));  
   }  
   $za->close();  
 }  
 else   
 {   
   echo 'Could not create a zip archive';  
 }  
 if ($download_file)  
 {  
   ob_get_clean();  
   header("Pragma: public");  
   header("Expires: 0");  
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");  
   header("Cache-Control: private", false);  
   header("Content-Type: application/zip");  
   header("Content-Disposition: attachment; filename=" . basename($zip_file_name) . ";" );  
   header("Content-Transfer-Encoding: binary");  
   header("Content-Length: " . filesize($zip_file_name));  
   readfile($zip_file_name);  
 }  
 if($delete_file_after_download)  
 {  
   unlink($zip_file_name);  
 }?> 

* If you like this post please don’t forget to subscribe Techies Badi - programming blog for more useful stuff