Loading feed...

PHP

Recursive PHP array SQL nested set
<?php
$nodes 
= array('with all the rows of the sql query nested set');
$menu $this->getSubTree($nodes);

function 
getSubTree(&$nodes$prevNodeRgt false){
    while(
$node current($nodes)){
        
// No more children? Continue one level up
        
if($prevNodeRgt && $prevNodeRgt $node['rgt']) return $tree;

        
next($nodes);

        
//Has children?   
        
if($node['rgt'] - $node['lft'] > 1$node['children'] = $this->getSubTree($nodes$node['rgt']);    

        
$tree[] = $node;        
    }
    return 
$tree
}
?>
PHP GZip encode
<?php
/**
* Gzip encode css/js files.
* Handy when server does not allow or provide this function
*/

//CSS to encode
gzip_encode(array('of css files'),
            
$_SERVER['DOCUMENT_ROOT'] . '/styles/%file%.css',
            
$_SERVER['DOCUMENT_ROOT'] . '/styles/gzip/%file%.css.gz');

//JS to encode
gzip_encode(array('of javascript files'),
            
$_SERVER['DOCUMENT_ROOT'] . '/js/%file%.js',
            
$_SERVER['DOCUMENT_ROOT'] . '/js/gzip/%file%.js.gz');                   

//Creates gzip compressed files. These files are used in production
function gzip_encode($files$uncompressed$compressed){
    foreach(
$files AS $file){
        
$uncompressedFilename str_replace('%file%'$file$uncompressed);
        
$compressedFilename str_replace('%file%'$file$compressed);
        if(!
is_file($compressedFilename) || (is_file($compressedFilename) && filemtime($uncompressedFilename) > filemtime($compressedFilename))){
            
file_put_contents($compressedFilenamegzencode(file_get_contents($uncompressedFilename)));
        }
    }
}

?>
WoL (Wakeup on LAN)
<?php
function wakeOnLan($ip$mac$port) {
    
$ip_byte explode(':'$mac);
    
$hw_addr '';
    for (
$a=0$a <6$a++) $hw_addr .= chr(hexdec($ip_byte[$a]));
    
$msg chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
    for (
$a 1$a <= 16$a++) $msg .= $hw_addr;
    
$s socket_create(AF_INETSOCK_DGRAMSOL_UDP);
    if (
$s == false) {
        echo 
'Error creating socket!\n';
        echo 
'Error code is "'.socket_last_error($s).'" - ' socket_strerror(socket_last_error($s));
        return 
FALSE;
    }
    else {
        if(
socket_sendto($s$msgstrlen($msg), 0$ip$port)) {
            echo 
'Magic Packet sent successfully!';
            
socket_close($s);
            return 
TRUE;
        }
        else {
            echo 
'Magic packet failed!';
            return 
FALSE;
        }
    }
}
?>
Ping device
<?php
if(isset($_GET['check'])){
    
$this->template false;
    
$address ='LAN-IP'//Here you can specify the address you want to check ports
    
$port 'PortToPing';           //Here you can specify the port you want to check from $address
    
$checkport = @fsockopen($address$port$errnum$errstr1); //The 1 is the time of ping in secs
    
@fclose($checkport);
    die;
}
?>
Drive space
<?php
/**
* Gets total space, free space for all drives (both in percentages and amounts in GB)
* IMPORTANT! Leave the @ on windows machines to prevent warnings
* @param: 67 = C, 90 = Z
*/
for($i 67$i <= 90$i++){
    
$drive strtolower(chr($i));
    if (
is_dir($drivePath $drive ':')){
        @
$drives[$drive]['total_space']      = number_format(disk_total_space($drivePath) / 1024 1024 10242);
        @
$drives[$drive]['free_space']       = number_format(diskfreespace($drivePath) / 1024 1024 10242);
        
//$drives[$drive]['used_space']       = number_format((disk_total_space($drivePath) / 1024 / 1024 / 1024) - (diskfreespace($drivePath) / 1024 / 1024 / 1024), 2);
        
@$drives[$drive]['free_percentage']  = number_format((diskfreespace($drivePath) / disk_total_space($drivePath)), 2);
        @
$drives[$drive]['used_percentage']  = number_format((disk_total_space($drivePath) - diskfreespace($drivePath)) / disk_total_space($drivePath), 2);                   
        
        @
$drives[$drive]['free_percentage'] = $drives[$drive]['free_percentage'] * 100 '%';
    }
}
?>
Generate custom $_GET array
<?php
/**
* This function takes the REQUEST_URI and fills up the $_GET array.
* Each directory part (that is /someUrlPart) will be put into $_GET numerically starting at zero.
* Each querystring parameter (that is ?someVariable=someValue) will be put into $_GET as $_GET[key] = value
* Handy to be used in conjunction with a htaccess redirect.
*/
function setGetArray(){
    
$tempUrl substr_replace($_SERVER['REQUEST_URI'], ''0strlen(BASE) + 1);
    
    
//Get all the directories being requested and put them into $_GET[] starting with 0
    
$DIRvars explode('/'$tempUrl);
    foreach(
$DIRvars AS $var){
        if((
$pos strpos($var'?')) !== false){
            
$_GET[] = substr($var0$pos);
        }
        else {
            
$_GET[] = $var;
        }
    }
    
    
//Get all the GET requests and put them into $_GET['key'] = 'value'
    
$GETvars explode('?'$tempUrl);
    if(isset(
$GETvars[1])){
        
$GETvars explode('='$GETvars[1]);
        for(
$i=0$i count($GETvars); $i++){
            if(
$i == 0){
                
$key $GETvars[$i];
            }
            else {
                
$_GET[$key] = $GETvars[$i];
            }
        }
    }
?>
Copyright © 2008 - 2012 Jos Nienhuis
Portfolio | C.V. | About me | Links | Contact | Sitemap | Settings
Powered by PHP PowerPlay © 2008 - 2012
Generated in 0.0875768661499 seconde
XHTML 1.0 transitional W3C valid RSS 2.0 W3C valid