How to delete non-empty directory with PHP?

2 04 2011

You can easily delete any empty directory with build-in function rmdir(), but it can’t delete any directory which contains some file within it. For that, you need to first delete the files inside the directory. The below function exactly does the same, it recursively delete any internal files/directories. Take a look at the code-

function deleteDirectory($directory)
{
	if(!$dh=opendir($directory))
	{
		return false;
	}
	
	while($file=readdir($dh))
	{
		if($file == "." || $file == "..")
		{
			continue;
		}
		
		if(is_dir($directory."/".$file))
		{
			deleteDirectory($directory."/".$file);
		}
		
		if(is_file($directory."/".$file))
		{
			unlink($directory."/".$file);
		}
	}
	
	closedir($dh);
	
	rmdir($directory);
}




Getting directory size with PHP

2 04 2011

This script will show you how easily you can get the directory size with php. The function is used recursively to get the inside directories. It will return directory size in byte. Here is the code -

function getDirectorySize($directory)
{
	$dirSize=0;
	
	if(!$dh=opendir($directory))
	{
		return false;
	}
	
	while($file = readdir($dh))
	{
		if($file == "." || $file == "..")
		{
			continue;
		}
		
		if(is_file($directory."/".$file))
		{
			$dirSize += filesize($directory."/".$file);
		}
		
		if(is_dir($directory."/".$file))
		{
			$dirSize += getDirectorySize($directory."/".$file);
		}
	}
	
	closedir($dh);
	
	return $dirSize;
}




How to add new file extension to parse php code?

28 03 2011

For some reason if you need to enable a new file extension rather than .php to parse your php code, then this article will help you. But before you proceed, i would like to warn you an issue. If you enable other file extensions like .html to parse php code, every time an html file is requested, it is passed to php for parsing. It may have an issue with performance. However, if you need to enable more extensions to parse php code, then continue reading.

The process is very simple, all you need is to add the specified extension in your apache configuration file, which is httpd.conf . So you need to locate the file. Open the file and find the line –

AddType application/x-httpd-php .php

If its not there, add the line and then add your desired extension after .php separated by space -
AddType application/x-httpd-php .php .html

Finally restart apache server. This way you can add other extensions. What it does is, it binds a MIME type to a particular extension.





Fix problem installing reCaptcha with table layout

12 03 2011

In my earlier post, i showed you how you can easily install reCaptcha in your php scripts. In this post i would like to mention a quick fix to those of you who have experienced an unusual error while trying to install it. If you have ever tried to install the reCaptcha in a form containing html table and put the form inside the table, then i have faced this situation, where every successful submission of form will tell you reCaptcha was incorrect like that “The reCAPTCHA was incorrect, try it again.”.

The fix is simple, just place the table inside the form and it will work.





How to display latest wordpress blog posts to non-wordpress site?

23 02 2011

You can easily display your latest wordpress blog posts to your non-wordpress site. For this, first you need to add the below line at the top of your php script-

<?php
require(./path/wp-blog-header.php');
?>

When the above line is added to your php script, it will turn your script into wordpress page, which means, you will be able to use any WordPress template tags in your script. Now we will see how can we retrieve few recent posts from our wordpress blog.

$recent_posts = wp_get_recent_posts(5);

wp_get_recent_posts() function will retrieve latest posts from your blog as an array, by default 10 posts are retrieved. To get any other number of posts, just pass the parameter as – wp_get_recent_posts(5) to get latest 5 posts. Now we will loop through the array to get each individual posts with title and url-

foreach($recent_posts as  $post){
    echo '<a href="' . get_permalink($post["ID"]) . '" title="Look '.$post["post_title"].'" >' .   $post["post_title"].'</a>';
}

Thats all you need to do. So here is the full source code-

<?php
require('./path/wp-blog-header.php');
?>
<ul>
  <li>
    <h2>Recent Posts</h2>
    <ul>
    <?php
      $recent_posts = wp_get_recent_posts(5);
      foreach($recent_posts as $post){
        echo '<li><a href="' . get_permalink($post["ID"]) . '" title="Look '.$post["post_title"].'" >' .   $post["post_title"].'</a> </li> ';
      } ?>
    </ul>
  </li>
</ul>




AutoKey:Linux desktop automation utility

3 01 2011

I was looking for a text expander program for linux environment, and found AutoKey, which actually does more than what i was looking for. Its a linux desktop automation utility which allows you to manage collection of scripts, and assign abbreviations and hotkeys to these scripts allowing you to execute them on demand in whatever program you are using. Its very simple to use the program. You should try this program. Click here to go to the project homepage of this program and start using it.





Use reCAPTCHA in your PHP Scripts

26 11 2010

With reCAPTCHA, you can easily provide captcha on you php powered sites. Simply follow the steps -

1. First you need to download reCAPTCHA PHP library, there you will find a php file named recaptchalib.php, which we will include later in our scripts.

2. Then sign up for API keys for your site. If you are planning to use the same key over all sub-domains, then you can enable the ‘global key’ option while signing up. You will be given a public key and a private key. Public key is for use in html/javascript code, that is served to end users. And private key is used in processing part, for communicating between your server and reCAPTCHA server.

3. Now time to show the captcha image in your site, just put the code block below inside the form tag where you wish captcha image to display -

require_once('recaptchalib.php');
$publickey = "your_public_key"; // you got this from the signup page
echo recaptcha_get_html($publickey);

4. And finally you need to verify the user input against the captcha image displayed, the following code block is responsible for that -

<?php
  require_once('recaptchalib.php');
  $privatekey = "your_private_key";
  $resp = recaptcha_check_answer ($privatekey,
                $_SERVER["REMOTE_ADDR"],
                $_POST["recaptcha_challenge_field"],
                $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA was incorrect, try it again.");
  } else {
    // Your code here to handle a successful verification
  }
?>

Thats all, it will surely save you from few hours of coding if were doing all this from scratch.








Follow

Get every new post delivered to your Inbox.