Categories

Count files in a directory

<?php
function count_files($name) 
{
$dirname = "$name";
if(is_dir($dirname))
$dir_handle = opendir($dirname);
if(!$dir_handle)
return false;

$files = 0;

while($file = readdir($dir_handle)) 
{
if($file != "." and $file != "..") 
{
if(!is_dir($dirname . "/" . $file))
$files++;
else
$files += count_files($dirname . "/" . $file);
}
}

closedir($dir_handle);

return $files;
}
?>

Create thumbnails of files in directory

<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
  // open the directory
  $dir = opendir( $pathToImages );
  // loop through it, looking for any/all PNG files:
  while (false !== ($fname = readdir( $dir ))) {
    // parse path for the extension
    $info = pathinfo($pathToImages . $fname);
    // continue only if this is a PNG image
    if ( strtolower($info['extension']) == 'png' )
    {
      //echo "Creating thumbnail for {$fname} <br />";
      // load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );
      // calculate thumbnail size
      $new_width = $thumbWidth;
	 $new_height = floor( $height * ( $thumbWidth / $width ) );
      // create a new temporary image
      $tmp_img = imagecreatetruecolor( $new_width, $new_height );
      // copy and resize old image into new image
      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
      // save thumbnail into a file
      imagepng( $tmp_img, "{$pathToThumbs}{$fname}" );
    }
  }
  // close the directory
  closedir( $dir );
}
?>

Users online

Create MySQL Table...

#
# Table structure for table 'useronline'
#

CREATE TABLE useronline (
   timestamp int(15) DEFAULT '0' NOT NULL,
   ip varchar(40) NOT NULL,
   file varchar(100) NOT NULL,
   PRIMARY KEY (timestamp),
   KEY ip (ip),
   KEY file (file)
);
<?php

$server = "xxx";
$db_user = "xxx";
$db_pass = "xxx";
$database = "xxx";
$timeoutseconds = 300;

$timestamp = time();
$timeout = $timestamp-$timeoutseconds;

mysql_connect($server, $db_user, $db_pass);
$insert = mysql_db_query($database, "INSERT INTO useronline VALUES ('$timestamp','$REMOTE_ADDR','$PHP_SELF')");
if(!($insert)) {
 print "Useronline Insert Failed > ";
}
$delete = mysql_db_query($database, "DELETE FROM useronline WHERE timestamp<$timeout");
if(!($delete)) {
 print "Useronline Delete Failed > ";
}
$result = mysql_db_query($database, "SELECT DISTINCT ip FROM useronline WHERE file='$PHP_SELF'");
if(!($result)) {
 print "Useronline Select Error > ";
}
$user = mysql_num_rows($result);


mysql_close();
if($user == 1) {
 print("<b>$user</b> user online\n");
} else {
 print("<b>$user</b> users online\n");
}

?>

Validate email

function is_valid_email($email, $test_mx = false)
{
	if(eregi("^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email))
		if($test_mx)
		{
			list($username, $domain) = split("@", $email);
			return getmxrr($domain, $mxrecords);
		}
		else
			return true;
	else
		return false;
}

Unzip Zip File

/**********************
*@file - path to zip file
*@destination - destination directory for unzipped files
*/
function unzip_file($file, $destination){
	// create object
	$zip = new ZipArchive() ;
	// open archive
	if ($zip->open($file) !== TRUE) {
		die (’Could not open archive’);
	}
	// extract contents to destination directory
	$zip->extractTo($destination);
	// close archive
	$zip->close();
	echo 'Archive extracted to directory';
}