Get File Extension

Very handy PHP function to get the file extension of a file name like something.jpg

Method 1: (best way)

1
2
3
4
5
6
7
8
9
10
11
// get_file_extension ( string [the file name] , boolean [lower case] )
function get_file_extension($str,$low = true){
	$i = strrpos($str,'.');
	if (!$i)
		return '';
	$l = strlen($str) - $i;
	$ext = substr($str,$i+1,$l);
	if($low == true)
	$ext = strtolower($ext);
	return $ext;
}

Method 2:

1
2
3
4
5
6
7
8
9
10
// get_file_extension ( string [the file name] , boolean [lower case] )
function get_file_extension($str,$low = true){
	$ar = explode('.',$str);
	if(empty($ar))
		return '';
	$ar = array_reverse($ar);
	if($low == true)
	$ext = strtolower($ar[0]);
	return $ext;
}

Both functions produce the same output but one uses the string functions and the other uses array function. If you think about it, to manipulate and create arrays take more memory then manipulating regular text. (internally arrays are created using those same string manipulation methods) So method one is better because it uses most simple way to get what you need.

Make Code

Very simple handy little function to generate a random code of number and lower and upper case letters.

Straight forward stuff…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function makecode($chars = 6)
{
	for($i=0; $i<=($chars-1); $i++)
	{
		$r0 = rand(0,1);
		$r1 = rand(0,2); // frequency of lower case
		if($r0==0)
			$r .= chr(rand(ord('A'),ord('Z')));
		elseif($r0==1)
			$r .= rand(0,9);
		if($r1==0)
			$r = strtolower($r);
	}
	return $r;
}

$r0 controls if the next digit is a number or a letter
$1 controls if teh letter will be upper or lower. Set it to 0 if you want it to always be lower or change the range to what ever suits you.
Also a number maybe passed to the function to change how many digits in the code to make.

Converting Bytes To Better Format

Every now and then we need a simple function that could take any number in bytes taken from filesize() or other source and convert it for better reading. Here are some functions that do the exact same thing but with different methods.

Method 1: Very simple straight forward stuff. Can be customized to different sizes with different limits.

1
2
3
4
5
6
7
8
function convert_size($num)
{
    if ($num >= 1073741824) $num = round($num / 1073741824 * 100) / 100 .' gb';
    else if ($num >= 1048576) $num = round($num / 1048576 * 100) / 100 .' mb';
    else if ($num >= 1024) $num = round($num / 1024 * 100) / 100 .' kb';
    else $num .= ' b';
    return $num;
}

Method 2: (best way) More advance using a loop to count size depth. Very small and compact all the way to YB. Unlike method 1 it runs by a single constant.

1
2
3
4
5
6
7
8
9
10
function convert_size($size){
  $i=0;
  $iec = array(" B", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
  while (($size/1024)>1)
  {
   $size=round($size/1024,2);
   $i++;
  }
  return substr($size,0,strpos($size,'.')+4).$iec[$i];
}

As you see if you wish to have a large range conversion using the first method then you will have lots of IF statements unlike method two. Iif you are using method one you have more simple flexibility to have different off-sets for sizes.

Method two uses a single while loop to take the size to its lowest by every 1024 and each time it loops it reduces a single multiple of 1024 and counts how many times it reduces it, and at the end shows the corresponding unit size from the array using that count. The array must have the unit sizes in correct order. Good thing about this is if you give it a number that is like 7^20 (that’s 20 zeroes), then it still works just fine as long as the array has that many unit sizes.

Displaying Page Load Time

It is very simple to show page load times in PHP.

Method 1:
Place this at the very begin of your PHP code:

1
define('STARTING_MICROTIME', get_microtime());

Depending on how you like to organize your code have those functions some where, they are pretty straight forward:

1
2
3
4
5
6
7
8
9
function execution_time()
{
    return sprintf("%01.4f", get_microtime() - STARTING_MICROTIME);
}
function get_microtime()
{
    $time = explode(' ', microtime());
    return doubleval($time[0]) + $time[1];
}

And finally just use the execution_time() function to show the total time elapsed since the script started.

Method 2:
Place this at very beginning of your code.

1
define('PAGE_LOAD_START', microtime(true));

And this where you want to display the render time.

1
<?php echo round((microtime(true) - PAGE_LOAD_START), 4); ?>

This way is easier IMO. Using constants is not necessary, it just avoids problems in namespaces if your code is OOP like.

Easy Image Class

I needed something simple to grab images from MySQL database and display them. Slowly it grew into a very handy class.

This class can be used to manipulate images stored in files or in a MySQL database. It can read images from files and store them in a MySQL database table, and vice-versa. The class can also convert images between GIF, JPEG and PNG formats, as well resize the images to create thumbnails.

Documentation:
Coming Soon…

MySQL BLOB Field

When I needed to know some basic information on BLOBs, I had very hard time finding it. Nearly every site I could not entirely understand at first glance. A BLOB is a “binary large object”; when using phpMyAdmin, you will see an upload form to insert a BLOB. Here is the maximum file sizes you can store in different blob fields in MySQL database.

Type Sizes
TINYBLOB 256 Bytes
BLOB 65,536 Bytes 64 KB
MEDIUMBLOB 16,777,216 Bytes 16,384 KB 16 MB
LARGEBLOB 4,294,967,296 Bytes 4096 MB 4 GB

Easy way to calculate exact sizes:
3 GB = 3 * 1024 * 1024 * 1024
4 MB = 4 * 1024 * 1024
2 KB = 2 * 1024

In many manuals for MySQL the limit is stated in characters, for BLOBs it is: 1 Character = 1 Byte

If you store a 2 MB picture in a MEDIUMBLOB and then change the field to BLOB, the picture will be cut off. Here is what you will get if you try opening teh image with gd library after fetching from database:

php php-cut-off

On the left is original uploaded or inserted image and on the right is what was actually stored from the inserted one and outputted with PHP GD library; using imagecreatefromstring() to make the image from the string fetched from the database.

Image Thumbnail / Resize Function

Log time ago I wrote this function for my friend’s gallery website. The function is straight forward and uses the GD library of PHP. You may resize the image by giving it the maximum width and height you want the thumbnail to have. The function acts as an extension of the GD library so to speak, so you have to pass the image resource. An additional argument may be passed stating if you want the thumbnail to be square or not, by default it is false. If it is true the width:height ratio will not be change, it only fills the empty space with black color and places the thumbnail image in the center of the square.

imagethumb(image resource, max width, max height, fill to make square)
1 – (GD image resource) Straight forward teh image resource loaded by GD library.
2 – (int) maximum width of thumbnail
3 – (int) maximum width of thumbnail
4 – (true/false) make thumbnail square

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function imagethumb($old_im, $limit_w = 100, $limit_h = 100, $fill = false)
{
	//get original dimensions
	$old_w = imagesx($old_im);
	$old_h = imagesy($old_im);
 
	//first resize by width if overflow else set old to new
	if($old_w > $limit_w)
	{
		//get reduction percent
		$reduce_percent = ($limit_w / $old_w) * 100;
		//set new w and h
		$new_w = ($old_w / 100) * $reduce_percent;
		$new_h = ($old_h / 100) * $reduce_percent;
	}
	else
	{
		//set as the old dimensions
		$new_w = $old_w;
		$new_h = $old_h;
	}
 
	//second resize by height if still overflows
	if($new_h > $limit_h)
	{
		//get reduction percent
		$reduce_percent = ($limit_h / $new_h) * 100;
		//set new w and h
		$new_w = ($new_w / 100) * $reduce_percent;
		$new_h = ($new_h / 100) * $reduce_percent;
	}
 
	if($fill == true)
	{
		//height offset picture, to keep centered
		if($new_h < $limit_h)
			$h_offset = floor(($limit_h - $new_h)/2);
		else
			$h_offset = 0;
 
		//width offset picture, to keep centered
		if($new_w < $limit_w)
			$w_offset = floor(($limit_w - $new_w)/2);
		else
			$w_offset = 0;
 
		//make new image and copy to it
		$new_im = imagecreatetruecolor($limit_w, $limit_h);
	}
	else
		$new_im = imagecreatetruecolor($new_w, $new_h);
 
	imagecopyresampled($new_im, $old_im, $w_offset, $h_offset, 0, 0, $new_w, $new_h, $old_w, $old_h);
 
	return $new_im;
}

New Training For The Job

In order to assure the highest levels of quality work and productivity from employees well trained through our program of SPECIAL HIGH INTENSITY TRAINING (S.H.I.T.). We are trying to give employees more S.H.I.T. then anyone else. If you feel that you do not receive your share of S.H.I.T. on the job, please see your manager. You will be immediately placed at the top of the S.H.I.T. list, and our managers are especially skilled at seeing that you get all the S.H.I.T. you can handle..

Employees that don’t take their S.H.I.T. will be placed in DEPARTMENTAL EMPLOYEE EVALUATION PROGRAMS (D.E.E.P. S.H.I.T.). Those who fail to take D.E.E.P. S.H.I.T. seriously will have to go to EMPLOYEE ATTITUDE TRAINING (E.A.T. S.H.I.T.). Since out managers took S.H.I.T. before they were prompted, they don’t have to do S.H.I.T. anymore, and are all full of S.H.I.T. already.

If you are full of S.H.I.T., you may be interested in a job training of others. We can add your name to our BASIC UNDERSTANDING LECTURE LIST (B.U.L.L. S.H.I.T.). Those who are full of B.U.L.L. S.H.I.T. will get the S.H.I.T. jobs, and can apply for promotion to DIRECTOR OF INTENSITY PROGRAMMING (D.I.P. S.H.I.T.).

If you have further questions, please direct them to our HEAD OF TRAINING, SPECIAL HIGH INTENSITY TRAINING (H.O.T. S.H.I.T.)

© SOTA Glazing Inc.

PHP Pagination Simple Mechanics

It is actually very simple. All you need to know is what page you are on and how many items there is to list in total. Here is an out line:

Step one, get those values:
total_items
items_per_page
current_page

Step two, calculate total pages:
total_pages = ( total_items / items_per_page )

Step three, calculate limit offset for database query:
start_item = current_page * items_per_page
end_item = if on last page then ( total_pages – start_item ) else ( items_per_page ) “asuming your database works like mysql”

Be mindful that arrays and databases start from 0 not from 1.

And from there just make your PHP code and add on nifty features of your own.

Check out my previous guide for a nice pagination script with demo.

PHP Website To Website File Transfer

My internet provider is not one of the best for one damn reason: It has bandwidth limit. So there I was wondering how am I going to transfer my 15gb website from old location to this shiny new one, knowing I have only 1GB on my monthly bandwidth left and it resets about 10 days from now. First attempt is to use one of those web ftp websites i found, well they all got their own bandwidth or they just freeze. I tried connecting to the old website using my own ftp script but it failed, and so did the remade scripts I tried using. The conclusion is that the old website does not allow direct ftp or special access, but my new one is liek a free world, do what you want sorta thing. Problem solved! I made mini script for teh old website that shows all files and folders in an orderly fashion in a big list. Then I made a simple file_get_content() and file_put_content() script on my new site. It was perfect, thought the rapid request rate shuts down the only connection between my two websites. Added a sleep(1) right after each download and flush() right after it is echo ‘file copied’ and it was done. Just sit back and relax as my new site copied all the files nice and fast. Continue reading ‘PHP Website To Website File Transfer’ »