PHP: CHMOD a directory recursively

The script below loops over the specified directory and chmods its files, directories and subdirectories recursively.

<?php

  function chmodDirectory( $path = '.', $level = 0 ){  
  $ignore = array( 'cgi-bin', '.', '..' ); 
  $dh = @opendir( $path ); 
  while( false !== ( $file = readdir( $dh ) ) ){ // Loop through the directory 
  if( !in_array( $file, $ignore ) ){

        if( is_dir( "$path/$file" ) ){

          chmod("$path/$file",0755);

          chmodDirectory( "$path/$file", ($level+1));

        } else {

          chmod("path/$file",0644); // desired permission settings

        }//elseif 
	}//if in array 
	}//while 
	
	closedir( $dh ); 
	}//function
	chmodDirectory("the_directory/",0);

?> 

PHP: How to create unique short string IDs with PHP & MySQL

PHP: How to create unique short string IDs with PHP & MySQL

PHP: How to create unique short string IDs with PHP & MySQL

IDs are often numbers. Unfortunately there are only 10 digits to work with, so if you have a lot of records, IDs tend to get very lengthy. For computers that’s OK. But human beings like their IDs as short as possible. So how can we make IDs shorter? Well, we could borrow characters from the alphabet as have them pose as additional numbers…. Alphabet to the rescue!

I created this function a long time ago. Time to be nice and share.

More is Less – the ‘math’

The alphabet has 26 characters. That’s a lot more than 10 digits. If we also distinguish upper- and lowercase, and add digits to the bunch or the heck of it, we already have (26 x 2 + 10) 62 options we can use per position in the ID.

Now of course we can also add additional funny characters to ‘the bunch’ like – / * & # but those may cause problems in URLs and that’s our target audience for now.

OK so because there are roughly 6x more characters we will use per position, IDs will get muchshorter. We can just fit a lot more data in each position.

This is basically what url shortening services do like tinyurl, is.gd, or bit.ly. But similar IDs can also be found at youtube: http://www.youtube.com/watch?v=yzNjIBEdyww
(more…)

CakePHP – jQuery Autocomplete Tutorial

CakePHP – jQuery Autocomplete Tutorial

CakePHP – jQuery Autocomplete Tutorial

In this tutorial, i will show you how to implement jQuery UI’s autocomplete widget in cakephp. The script in this tutorial is copy paste from tutorialzine article “A Simple Movie Search App w/ jQuery UI” . We are using a MySql database containing a users table. When you start typing a user name in the text box of the search form, an AJAX request is sent to controller. The controller returns a JSON object with suitable user name.

This is what we’re going to create:

CakePHP – jQuery Autocomplete Tutorial

CakePHP – jQuery Autocomplete Tutorial


(more…)

PHP: Adding Parameters To A URL in PHP

PHP: Adding Parameters To A URL in PHP

Adding Parameters To A URL in PHP

This article shows how to add query parameters to a URL in a php script. It is aimed at the beginner to intermediate level PHP programmer but developers from other languages may find some useful information here. At first this seems like a simple enough task. Starting with the url as a string, just add the new parameters to the end. Yeah, that would be nice. It turns out that there are a few conditions that make the process just a bit more difficult. It’s still not a hard problem but there are enough special cases to make it interesting.
(more…)

CakePHP: Configuring Apache

CakePHP: Configuring Apache

CakePHP: Configuring Apache

There is some tweaking that we need to perform in order to make sure that Apache runs CakePHP applications smoothly. Many Apache installations may not require the following tweaking, as they might be set as default, but it is always a good idea to check if the following settings are present.
(more…)

CakePHP: Understanding the MVC Pattern

CakePHP: Understanding the MVC Pattern

CakePHP: Understanding the MVC Pattern

The MVC (Model View Controller) pattern is a commonly used design pattern in software development, where the code is separated into three major parts: models, views, and controllers. The exact purpose of each part depends on the implementation, as it may vary from one framework to another. Here, we are going to describe the way CakePHP implements the MVC pattern. So, this is not a general discussion on MVC pattern, rather we are only going to see Cake’s own MVC implementation. As we have already mentioned, CakePHP separates the code into three separate parts: models, views, and controllers.
(more…)

What is CakePHP?

What is CakePHP?

What is CakePHP?

What is CakePHP?

According to the official CakePHP website (http://cakephp.org):
“Cake is a rapid development framework for PHP which uses commonly known design patterns like Active Record, Association Data Mapping, Front Controller and MVC. Our primary goal is to provide a structured framework that enables PHP users at all levels to rapidly develop robust web applications, without any loss to flexibility.”
Someone who is new to frameworks might not understand parts of the definition. But understanding a few key terms will make the job much easier.
(more…)

PHP: Getting the difference between two time strings

To get the difference between two time strings use this code

<?php

function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

//Usage :
echo timeDiff("2002-04-16 10:00:00","2002-03-16 18:56:32");

?>

PHP: How to Get the Current Page URL

If you want to get the current page URL, you can use the following code:

<?php
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER&#91;"HTTPS"&#93; == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER&#91;"SERVER_PORT"&#93; != "80") {
  $pageURL .= $_SERVER&#91;"SERVER_NAME"&#93;.":".$_SERVER&#91;"SERVER_PORT"&#93;.$_SERVER&#91;"REQUEST_URI"&#93;;
 } else {
  $pageURL .= $_SERVER&#91;"SERVER_NAME"&#93;.$_SERVER&#91;"REQUEST_URI"&#93;;
 }
 return $pageURL;
}
?>
<?php
  echo curPageURL();
?>

Pin It on Pinterest