BigToach.com

PHP Snippets

1 ... « 7 8 9 10 »

upload_files

Information

(PHP Version >= 4.0.3)
Functions used in this snippet: strlen, is_array, is_dir, is_writable, isset, sizeof, in_array, substr, strtolower, strrpos, move_uploaded_file.

Description

array upload_files( str form, str location, mixed types )

Upload any file that matches types filetypes, and send the files from the form name form to the file location of location.

form is the name of the form from the page where the uploads are coming from. This is the only required arguement to the function.

location defaults to the current working directory, but it can be set to any other directory that can be written to.

types are filetype extensions that are allowed. These must be lower case. The types can be either a array or a str. If you want to set types but leave the default location you can set location to NULL

The function returns an array of what happened with the upload. The returned values are mimetype, error code, whether the file was uploaded, file matches the types, filename, and filesize.

Note: This only checks file extensions. You may want to add additional checks of mimetypes to restrict data being loaded onto your server.

Snippet

<?php
function upload_files($form$location='./'$types=array()){
    
$ret = array();
    
$location $location === NULL || $location === '' './' $location;
    
$location $location{(strlen($location) - 1)} === '/' $location $location '/';
    
$types is_array($types) ? $types : array($types);
    if(!
is_dir($location) || !is_writable($location) || !isset($_FILES[$form])){
        return;
    }
    if(
is_array($_FILES[$form]['error'])){
        foreach(
$_FILES[$form]['error'] as $key=>$value){
            
$ret[$key] = array('uploaded'=>false'error'=>$value'typematch'=>false'name'=>$_FILES[$form]['name'][$key], 'mime'=>$_FILES[$form]['type'][$key], 'size'=>$_FILES[$form]['size'][$key]);
            if(
$value == UPLOAD_ERR_OK) {
                
$ret[$key]['uploaded']  = true;
                if(
sizeof($types) === || in_array(substr($_FILES[$form]['name'][$key], strtolower(strrpos($_FILES[$form]['name'][$key], '.')+1)), $types)){
                    
move_uploaded_file$_FILES[$form]['tmp_name'][$key], $location $_FILES[$form]['name'][$key]);
                    
$ret[$key]['typematch'] = true;
                }
            }
        }
    }else{
        
$ret = array('uploaded'=>false'error'=>$_FILES[$form]['error'], 'typematch'=>false'name'=>$_FILES[$form]['name'], 'mime'=>$_FILES[$form]['type'], 'size'=>$_FILES[$form]['size']);
        if(
$_FILES[$form]['error'] == UPLOAD_ERR_OK) {
            
$ret['uploaded'] = true;
            if(
sizeof($types) === || in_array(strtolower(substr($_FILES[$form]['name'], strrpos($_FILES[$form]['name'], '.')+1)), $types)){
                
move_uploaded_file$_FILES[$form]['tmp_name'], $location $_FILES[$form]['name']);
                
$ret['typematch'] = true;
            }
        }
    }
    return 
$ret;
}
?>

Example

upload_files Can be used in the following way:

<?php
// make form as a single file upload with formname 'file'
$files upload_files('file');
// uploads the single file to the current directory no matter the filetype

// make form as a single upload with formname 'image'
$files upload_files('image''./images/', array('gif''jpg''jpeg''png'));
// uploads the single file to the images directory with file extensions of gif, jpg, jpeg, or png

// make a form as an array upload with formname 'images'
$files upload_files('images',NULL,'png');
// uploads multiple files to the current directory as long as the file extension is png
?>

Added on Jun 29th at 3 am by Scott - 2 Comments
Updated on Jun 29th at 3 am.


site_is_up

Information

(PHP Version >= 3)
Functions used in this snippet: fsockopen, str_replace, fclose.

Description

bool site_is_up( str site, int port0, int timeout )

checks site on port port to see if the site is accepting connections (whether the site is up or not). If site is down it will take [i]timeout[i] seconds to timeout. Returns true on success and false on failure.

Snippet

<?php
function site_is_up($site,$port=80,$timeout=1){
    
$port $port === NULL 80 $port;
    
$fp = @fsockopen(str_replace('http://','',$site), $port$errno$errstr$timeout);
    if(
$fp === false){
        return 
false;
    }
    
fclose($fp);
    return 
true;
}
?>

Example

site_is_up Can be used in the following way:

<?php
if(site_is_up('www.bigtoach.com')){
    echo 
'Site is up!!';
}else{
    echo 
'Site is down.';
}
// self explanitory I hope

if(site_is_up('bigtoach.com',NULL,5)){ echo 'UP!'; }
// check bigtoach.com with a 5 second timeout (still on port 80)

if(site_is_up('bigtoach.com',2082)){ echo 'UP!'; }
// check bigtoach.com on port 2082
?>

Added on Jun 27th at 6 pm by Scott - 0 Comments
Updated on Jun 27th at 6 pm.


resize

Information

(PHP Version >= 4.0.6)
Functions used in this snippet: file_exists, substr, strrpos, stristr, imagecreatefromgif, imagecreatefromjpeg, imagecreatefrompng, imageSX, imageSY, ceil, imageCreatetruecolor, imagecreatetrueColor, imagecopyresized, isset, strtolower, imagecopy, imagedestroy.

Description

int resize( str image, int x [, int y [, str wm [, str wml]]])

Uses image to resize based on the size specification of x and y. If y is set to NULL the image will be resized to have its largest dimension be x pixels high/wide.
If wm is specified, it will be used as a watermark image to be placed over the resized image in the region wml. Where the first character places the watermark in the t - Top or b - Bottom and the second character places the watermark on the l - Left or r - Right side of the image.

Snippet

<?php
function resize($image,$x,$y=NULL,$wm=NULL,$wml='br'){
    if(!
file_exists($image)){
        return 
false;
    }
    
$images = array();
    if(
$wm !== '' && $wm !== NULL && file_exists($wm)){
        
$images['wmimg'] = $wm;
    }
    
$images['img'] = $image;
    foreach(
$images as $key=>$value){
        
$type substr($value,strrpos($value,'.'));
        if(
stristr($type,'i')){
            $
$key imagecreatefromgif($value);
        }
        if(
stristr($type,'j')){
            $
$key imagecreatefromjpeg($value);
        }
        if(
stristr($type,'n')){
            $
$key imagecreatefrompng($value);
        }
    }
    
$size = array();
    if(
$y === '' || $y === NULL){
        
$size['x'] = imageSX($img);
        
$size['y'] = imageSY($img);
        if(
$size['x'] >= $size['y']){
            
$size['dest_x'] = $x;
            
$size['dest_y'] = ceil($size['y'] * ($x $size['x']));
        }else{
            
$size['dest_y'] = $x;
            
$size['dest_x'] = ceil($size['x'] * ($x $size['y']));
        }
        
$dest imageCreatetruecolor($size['dest_x'],$size['dest_y']);
    }else{
        
$dest imagecreatetrueColor($x,$y);
        
$size['x'] = imageSX($img);
        
$size['y'] = imageSY($img);
        
$size['dest_x'] = $x;
        
$size['dest_y'] = $y;
    }
    
imagecopyresized($dest$img0000$size['dest_x'], $size['dest_y'], $size['x'], $size['y']);
    if(isset(
$wmimg)){
        
$size['wmx'] = imageSX($wmimg);
        
$size['wmy'] = imageSY($wmimg);
        
$size['wmh'] = strtolower($wml{0}) === 'b' ? ($size['dest_y'] - $size['wmy'] - 2) : 2;
        
$size['wmw'] = strtolower($wml{1}) === 'r' ? ($size['dest_x'] - $size['wmx'] - 2) : 2;
        
imagecopy($dest$wmimg$size['wmw'], $size['wmh'], 00$size['wmx'], $size['wmy']);
        
imagedestroy($wmimg);
    }
    
imagedestroy($img);
    return 
$dest;
}
?>

Example

resize Can be used in the following way:

<?php
header
('Content-type: image/jpeg'); // set your header for jpeg image
imagejpeg(resize('imagefile.png',100));
// outputs imagefile.png as a jpeg constrained to 100 px with its ratio kept the same

header('Content-type: image/png'); // set your header for png image
imagepng(resize('filename.png',100,100));
// outputs filename.png as a png and as a 100X100 image

header('Content-type: image/png'); // set your header for png image
imagepng(resize('imagefile.jpg',100,NULL,'logo.png','bl'));
// outputs imagefile.jpg as a png constrained to 100 px with its ratio kept the same
// it also adds logo.png on top of the image on the bottom left of the image
?>

Added on Jun 26th at 11 pm by Scott - 1 Comment
Updated on Jun 27th at 5 pm.


stats

Information

(PHP Version >= 4.3.0)
Functions used in this snippet: file_exists, file_get_contents, unserialize, date, isset, str_replace, time, is_array, unset, strtotime, count, fopen, fwrite, serialize, fclose.

Description

array stats( str file)

Returns an array holding the values of hits today, hits yesterday, users currently online and total number of hits. The function stores the data in the file file.
On failure the functions returns false

Snippet

<?php
function stats($file,$interval=1){
    if(
file_exists($file)){
        
$str file_get_contents($file);
        
$array unserialize($str);
        
$array is_array($array) ? $array : array();
        
$array['dayhits'][date('dmy')] = isset($array['dayhits'][date('dmy')]) ? $array['dayhits'][date('dmy')]+1;
        
$array['online'][str_replace('.''_'$_SERVER['REMOTE_ADDR'])] = time();
        
$array['alltime'] = isset($array['alltime']) ? $array['alltime']+1;
        if(
is_array($array['online'])){
            foreach(
$array['online'] as $ip=>$time){
                if(
$time < (time() - ($interval 60))){
                    unset(
$array['online'][$ip]);
                }
            }
        }else{
            
$array['online'] = array();
        }
        
$ret['hits'] = $array['alltime'];
        
$ret['today'] = $array['dayhits'][date('dmy')];
        
$yesterday date('dmy'strtotime('-1 day'));
        
$ret['yesterday'] = isset($array['dayhits'][$yesterday]) ? $array['dayhits'][$yesterday] : 0;
        
$ret['online'] = count($array['online']);
        
$fp fopen($file'w');
        
fwrite($fpserialize($array));
        
fclose($fp);
        
print_r($array);
        return 
$ret;
    }
    return 
false;
}
?>

Example

stats Can be used in the following way:

<?php
$stats 
stats('stats.txt');
echo 
'There have been ' $stats['today'] . ' hits today. There were ' $stats['yesterday'] . ' hits yesterday. There are a total of ' $stats['hits'] . ' hits and there is currently ' $stats['online'] . ' users on this page.';
?>

Added on Jun 27th at 7 am by Scott - 0 Comments
Updated on Jun 27th at 7 am.


get_ff

Information

(PHP Version >= 4.3.0)
Functions used in this snippet: file_exists, unserialize, file_get_contents, is_array.

Description

array get_ff( str file)

Reads file contents and unserializes them to form a flat-file array. If the file cannot be read, or the contents of the file is not serialized, an empty array is returned.

Snippet

<?php
function get_ff($file){
    if(
file_exists($file)){
        
$array = @unserializefile_get_contents$file ) );
$array is_array($array) ? $array : array();
        return 
$array;
    }
    return array();
}
?>

Example

get_ff Can be used in the following way:

<?php
$array 
get_ff('somefile.txt');
print_r($array); // outputs array stored in somefile.txt

$array get_ff('non-existing-file.ext'); 
print_r($array); // outputs an empty array
?>

Added on Jun 26th at 7 am by Scott - 0 Comments
Updated on Jun 27th at 7 am.


1 ... « 7 8 9 10 »